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.