nullptr
In its simplest form, nullptr replaces NULL and a literal zero, but there is more.
Since the days when C was invented, the C-based language family has had an uneasy truce with the concept of nothingness. C gave us the NULL macro, often defined as:
#define NULL ((void *)(0))
C++ doesn't go for the sledgehammer cast operation of just forcing the reinterpretation of the bit pattern for zero. C++ also likes to think of (void *) as being the base class object of all pointers of any type, and confusing it with an integer zero does not give us the clarity we have in all the other parts of pointer semantics and syntax.
The nullptr keyword resolves the ambiguity, and has the following properties:
- nullptr represents a value different from any valid pointer.
- nullptr can be assigned to and compared with any pointer or pointer-to-member.
- nullptr can be used to initialze pointer values.
- nullptr cannot be converted (accidentally or deliberately) to an integer.
- nullptr cannot be involved in a pointer-arithmetic expression, which was a common enough bug in C++ ... starting with a null pointer and calculating an offset from it.
At least in Gnu C++ 4.4, nullptr has yet to be implemented, although it strikes me as one of the easier changes. I will try to remember to update this page as the Gnu compiler moves forward.
Last updated 2014-07-19T15:44:11+00:00.
Links to the standard
A discussion of nullptr can be found in section 18.2.
Benefits
At long last, C++ has a true representation of nothingness that cannot be confused with another type.
Risks
If you start using it now, you will need to use the preprocessor to #define a meaning for it. When it gets implemented in compilers, you will need to remove the #define because it will negate the correctly implemented feature of the language.