Skip to content

nullptr (C++11) ​

In a Nutshell ​

A null pointer literal of type std::nullptr_t that safely distinguishes integer overloads, completely resolving the ambiguity the NULL macro and the integer 0 create in templates and function overloading.

No header required (it is a language keyword); the type is defined in <cstddef>.

Core API Quick Reference ​

OperationSignatureDescription
Null pointer literalnullptrA prvalue of type std::nullptr_t
Implicit conversion→ any pointer typeConverts to the null pointer value of the corresponding type
Implicit conversion→ any member pointer typeConverts to the null member pointer value of the corresponding type

Minimal Example ​

C++
#include <iostream>
void f(int) { std::cout << "int\n"; }
void f(int*) { std::cout << "int*\n"; }

int main() {
    f(0);        // Calls f(int), possibly unintended
    f(nullptr);  // Calls f(int*), exact match
    int* p = nullptr;
    if (p == nullptr) { std::cout << "null\n"; }
}

Embedded Applicability: High ​

  • A zero-overhead abstraction: the compiler directly produces a null pointer value at compile time, generating the same instructions as 0 or NULL
  • Avoids integer-vs-pointer overload ambiguity in register-manipulation functions (e.g., overloads that operate on hardware registers)
  • Behaves correctly in template metaprogramming (e.g., static assertions, type traits), where NULL and 0 would fail
  • Fully compatible with C-style low-level hardware-manipulation code, so NULL can be replaced incrementally with no risk

Compiler Support ​

GCCClangMSVC
4.63.02010

See Also ​


Some content referenced from cppreference.com, licensed under CC-BY-SA 4.0

pdf-latest-70-g3de3d0a · 3de3d0a · 2026-09-27