2011-10-19 CPlusPlus0X

New keyword nullptr

Maybe what can be called a smaller correction in the new standard is the introduction of the keyword nullptr. In C++ 0 is allowed as the value for a null pointer. Often the macro NULL, defined to 0, is used to express that this is a pointer.

However 0 is of type int. So in a overloading scenario like this:

void func(int);
void func(char *);

The call, func(NULL), will call func(int)!

This might not be an overload situation that happens that often but still it is a problem. Maybe it is equally important that there now is a keyword that truly is a null pointer constant. No need to use NULL nor 0 any more for pointers.

Further nullptr has the semantics that you would expect with pointers. Here backward compatibility was regarded as important. So a nullptr in a bool context is false. Like this:

bool trueValue = !nullptr;
bool falseValue = nullptr;

Note that the constant 0 is still a valid null pointer constant for backwards compatibility reasons. So the nullptr keyword is not likely to break old code.