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 caused by the macro NULL and the integer 0 in templates and function overloading.

No header required (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 a null pointer value of the corresponding type
Implicit conversion→ Any member pointer typeConverts to a null member pointer value of the corresponding type

Minimal Example

cpp
void f(int* p) {
    // Handle pointer
}

void f(int i) {
    // Handle integer
}

int main() {
    // Calls f(int*)
    f(nullptr);

    // Calls f(int)
    f(0);
}

Embedded Applicability: High

  • A zero-overhead abstraction; the compiler directly generates a null pointer value, producing the same instructions as 0 or NULL.
  • Avoids ambiguity between integer and pointer overloads in register manipulation functions (e.g., overloads for hardware registers).
  • Behaves correctly in template metaprogramming (e.g., static assertions, type traits), whereas NULL and 0 would fail.
  • Fully compatible with C-style low-level hardware manipulation code, allowing for risk-free, gradual replacement.

Compiler Support

GCCClangMSVC
4.63.02010

See Also


部分内容参考自 cppreference.com,采用 CC-BY-SA 4.0 许可

v0.7.0-9-g940ec1b · 940ec1b · 2026-07-05