2

I just tried to initialised a container, which happened to be empty and came across the following phenomenon:

#include <iostream>
#include <array>
#include <algorithm>

int main(int argc, char *argv[])
{
  std::array<int,NULL> foo = {};
  if ( std::all_of(foo.begin(), foo.end(), [](int i){return i==0;}) )
  std::cout << "All the elements are zero.\n";
  return 0;
}

compiling with:

clang++ -std=c++11 -stdlib=libc++ -o test test.cpp

resulted in:

bash-3.2$ ./test
All the elements are zero.

I am trying to figure out why an empty container returns true for this operation. This problem might be related to: Behaviour of std::list:begin() when list is empty

However I could not find a proper answer to this particular question.

Thank you for your time.

5
  • Don't use NULL as an integer constant. Commented Sep 1, 2014 at 7:25
  • @T.C. thank you for the tip. I actually tried 'std::array<int,0> foo = {};' the result was the same. Commented Sep 1, 2014 at 7:32
  • 1
    Yes, the result would be the same. That's not the point. NULL is intended to be used as a pointer constant. If you collaborate on software projects with other people, and you use NULL for things other than what it was intended, you will waste your collaborators' time as they try to understand why there is a NULL there. Commented Sep 1, 2014 at 7:38
  • @BenjaminLindley Post-C++11, an implementation is also allowed to define NULL to be nullptr instead. So it's not even portable. Commented Sep 1, 2014 at 7:44
  • @Benjamin - thank you for the explanation. I am still in the early learning process of cpp so I really appreciate any help to avoid future mistakes. Commented Sep 1, 2014 at 7:44

3 Answers 3

6

std::all_of returns true if the range is empty. From 25.2.1 All of

template <class InputIterator, class Predicate>
bool all_of(InputIterator first, InputIterator last, Predicate pred);

Returns: true if [first,last) is empty or if pred(*i) is true for every iterator i in the range [first,last), and false otherwise.

Sign up to request clarification or add additional context in comments.

Comments

5

I am trying to figure out why an empty container returns true for this operation.

Because it is true. There are zero elements in the container which are not equal to 0. Therefore all of the elements satisfy the condition of being equal to zero. They also all happen to satisfy the condition of not being equal to 0, since there are zero elements in the container which are equal to 0, so the reverse predicate would work as well.

In fact, there are zero elements in the container which fail to satisfy any condition at all, therefore all of the elements of the container satisfy any condition. So any predicate passed to all_of will result in all_of returning true for an empty range. Even a predicate which just returns true or false, irrespective of its argument.

Comments

3

It's correct, it's behaviour of std::all_of algorithm.

Return value: true if unary predicate returns true for all elements in the range, false otherwise. Returns true if the range is empty.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.