4

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.

Community
  • 1
  • 1
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • `gcc doesn't highlight` ==> `VIM editor doesn't highlight in gcc` :) – iammilind Jul 12 '11 at 12:36
  • 2
    Your gcc isn't going to "highlight" `nullptr`. gcc is a compiler; the only presentation from the compiler are the error and warnings that result from compilation; hopefully, there are none. It is your editor is that is going to highlight nullptr. It shouldn't be all that surprising your editor isn't up to speed with the new standard because the new standard doesn't really exist yet, at least not officially. That's why it is still called C++0x rather than C++11. – David Hammen Jul 12 '11 at 12:36
  • 2
    See [What exactly is nullptr in C++0x?](http://stackoverflow.com/questions/1282295/what-exactly-is-nullptr-in-c0x/1282345#1282345). Some of your questions are answered there. – Eran Jul 12 '11 at 12:38
  • For vim C++0x syntax see here: http://stackoverflow.com/questions/2977174/is-there-a-c0x-syntax-file-for-vim – René Richter Jul 12 '11 at 12:54
  • 1
    @eran: I think with 3k+ rep you should already be able to vote for closing as an exact duplicate (which will in turn add a comment similar to the one you added). Thanks for pointing out the duplicate anyway! – David Rodríguez - dribeas Jul 12 '11 at 13:02

5 Answers5

10

It is a keyword, the standard draft says (lex.nullptr):

The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t.

the nullptr is 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 (consider int& a = 1.f;!).

Doing #define NULL nullptr shouldn't alter the behaviour unless you did use NULL in a context such as int i = 4; if(NULL == i) {}, which won't work with nullptr because nullptr is 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 because nullptr needs a type.

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
  • +1, although "shouldn't alter the behaviour unless ..." - in other words, it does break (some) things. `NULL` is guaranteed to have some integer type, `nullptr` isn't. – Steve Jessop Jul 12 '11 at 13:47
  • 2
    There is a small use for `nullptr_t`. If you have several overloads taking different pointer types, then the `nullptr`-to-pointer cast will be ambiguous. You can use a `nullptr_t` overload to disambiguate the call. –  Jul 16 '11 at 21:51
6

nullptr is a keyword that represents null pointer constant. It is of type nullptr_t, which is implicitly convertible and comparable to any pointer type or pointer-to-member type.

Read these,

Nawaz
  • 353,942
  • 115
  • 666
  • 851
4

nullptr is indeed a keyword and the standard demands a type std::nullptr_t to be equivalent to typedef decltype(nullptr) nullptr_t; to enable overloading based on nullptr.

Xeo
  • 129,499
  • 52
  • 291
  • 397
3

nullptr will be a keyword in next C++ standard, now called C++0x.

It is needed to disambiguate between f(int) and f(T*), so it's not simply 0, but of nullptr_t.

I didn't know gcc can highlight code ;-)

René Richter
  • 3,839
  • 3
  • 34
  • 42
3

nullptr is not an object just like 0 is not an integer object. The former is a prvalue (i.e. a kind of expression) of type std::nullptr_t and the latter is an integer literal (also a kind of expression and also a prvalue) of type int.

It is possible to initialize an object with such expressions:

void* p = nullptr;
int i = 0;

It is not possible to initialize an lvalue reference with such expressions because they are prvalues; an lvalue reference can only be initialized from an lvalue.

void*& p = nullptr; // Invalid
int& i = 0; // Invalid
Luc Danton
  • 34,649
  • 6
  • 70
  • 114