Skip to content

Trivia impact: real

The Null With a Type

a null pointer literal that is neither an integer nor a pointer

auto nothing = nullptr;
std::cout << std::boolalpha
          << std::is_same<decltype(nothing), std::nullptr_t>::value << '\n';

What type does auto deduce for nullptr?

Answer
true

It deduces std::nullptr_t, the distinct type of the null pointer literal.

Why

[lex.nullptr] defines nullptr as a null pointer literal whose type is std::nullptr_t. That type converts to any pointer type, but it is not itself a pointer type and it is not the integer literal 0. Since no conversion is needed for auto deduction, nothing keeps the literal's own type.

Where it shows up

This matters in generic code, overload resolution, and type traits. It is also the reason nullptr is a safer null argument than 0 or a platform-defined NULL macro.

Takeaway: nullptr is a value of its own type, std::nullptr_t.

Try it: g++ -std=c++17 main.cpp -o demo && ./demo

Open in Compiler Explorer ↗ Quiz this entry