8

Now that C++0x is almost here, I've been experimenting with it, and in particular using nullptr. I haven't been able to figure out what standard header files one is supposed to include if one needs to use it.

Any help is appreciated.

swestrup
  • 4,079
  • 3
  • 22
  • 33

1 Answers1

20

No headers should be required. It is a built-in keyword (§[lex.nullptr]).

2.14.7 Pointer literals                 [lex.nullptr]

pointer-literal:
        nullptr

The pointer literal is the keyword nullptr. It is a prvalue of type std::nullptr_t. [ Note: std::nullptr_t is a distinct type that is neither a pointer type nor a pointer to member type; rather, a prvalue of this type is a null pointer constant and can be converted to a null pointer value or null member pointer value. See 4.10 and 4.11. —endnote]


Its type, std::nullptr_t, however, is "defined" in the header <cstddef> (§[support.types]/9).

nullptr_t is defined as follows:

namespace std {
    typedef decltype(nullptr) nullptr_t;
}

The type for which nullptr_t is a synonym has the characteristics described in 3.9.1 and 4.10. [Note: Although nullptr’s address cannot be taken, the address of another nullptr_t object that is an lvalue can be taken. —endnote]

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • Thanks! I had originally thought so, but discovered that g++ had stopped accepting nullptr in one of my unit tests. I thought it was a header issue, but somehow my version of g++ had been degraded from 4.6 (which supports it) to 4.5 (which doesn't). – swestrup Apr 05 '11 at 06:16
  • 12
    heh, I love the backwards logic of defining the type of `nullptr` to be "the type of `nullptr`. – jalf Apr 05 '11 at 06:39