Possible Duplicate:
What exactly is nullptr?
I first thought it’s a keyword. My present gcc doesn’t highlight nullptr in a different shade. To verify that, I wrote following:
void *&p = nullptr;
So I got some clue from the error that:
error: invalid initialization of non-const reference of type ‘void*&’
from an rvalue of type ‘std::nullptr_t’
If nullptr is an object then is it really a pointer equivalent of simple 0? In other word, suppose I write:
#define NULL nullptr
Is the above statement doesn’t alter anything in my code ? Also, it would be interesting to know other use cases for std::nullptr_t type as such.
It is a keyword, the standard draft says (lex.nullptr):
the
nullptris not yet a pointer, but it can be converted to a pointer type. This forbids your above assignment, which is an assignment to an unrelated reference type, in which case no conversion is possible (considerint& a = 1.f;!).Doing
#define NULL nullptrshouldn’t alter the behaviour unless you did useNULLin a context such asint i = 4; if(NULL == i) {}, which won’t work withnullptrbecausenullptris can’t be treated as an integer literal.I don’t think there are many other use-cases for
std::nullptr_t, it’s just a sentinel becausenullptrneeds a type.