tl;dr: Use nullptr, or define your own equivalent.
The problem is that NULL is some macro that expands to an integral constant expression with value zero. In order to call the function, std::find has to deduce the type and use the value (0).
You cannot compare int* with int, hence the error. As far as the function is concerned, you just passed it some regular old int that happens to be zero, and those cannot be converted to null pointers; they have to be integral constant expressions.
Normally NULL "works" because it's used in contexts where it isn't treated just as its integer form, e.g.:
if (ptr == NULL)
Because here it maintains its "integral constant expression" status, so converts to a null pointer of the compared-to type.
You should use nullptr if you're in C++11 or beyond, because this is actually a null pointer, not an integer that converts to it. What you've described is actually one of the motivating factors for introducing nullptr.
There are various C++03 implementations of nullptr if you need one. I have attached the classic implementation at the bottom of this answer.
Also, you should prefer std::array if possible (Boost has one if you need it), or at the very least use std::begin and std::end to get the array begin and end pointers (and again, there are implementations of this floating around):
#include <algorithm>
#include <array>
int main() {
std::array<int*, 8> foo = {};
std::find(foo.begin(), foo.end(), nullptr);
}
All said, in a pinch casting to the null pointer of your type is a valid solution. nullptr is really just a short-hand for "a thing that converts to the null pointer of the needed type".
Here is a nullptr implementation, initially created by Scott Meyers:
const
struct nullptr_t {
template <typename T>
operator T*() const {
return 0;
}
template <typename C, typename T>
operator T C::*() const {
return 0;
}
private:
void operator&() const;
} nullptr = {};
The syntax looks a bit funny because we don't usually define a class and a variable at the same time. Obviously if you want to stay C++11 compatible, nullptr isn't a usable identifier. null_ptr or nullpointer are good alternatives.
nullptredited out as it has nothing to do with the question.NULLis0, and doesn't have pointer type and is deduced as integer type, thus the error.static_cast<int*>(NULL)is suitable, yes