正常
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.
Header
No header required (it is a language keyword); the type is defined in <cstddef>.
Core API Quick Reference
| Operation | Signature | Description |
|---|---|---|
| Null pointer literal | nullptr | A prvalue of type std::nullptr_t |
| Implicit conversion | → any pointer type | Converts to the null pointer value of the corresponding type |
| Implicit conversion | → any member pointer type | Converts 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
0orNULL - 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
NULLand0would fail - Fully compatible with C-style low-level hardware-manipulation code, so
NULLcan be replaced incrementally with no risk
Compiler Support
| GCC | Clang | MSVC |
|---|---|---|
| 4.6 | 3.0 | 2010 |
See Also
Some content referenced from cppreference.com, licensed under CC-BY-SA 4.0