nullptr,指针字面量 (C++11 起)
目录 |
[编辑] 语法
nullptr
|
|||||||||
[编辑] 解释
关键词 nullptr 代表指针字面量。它是 std::nullptr_t 类型的纯右值。存在从 nullptr 到任何指针类型及任何成员指针类型的隐式转换。同样的转换对于任何空指针常量也存在,空指针常量包括 std::nullptr_t 的值,以及宏 NULL。
[编辑] 关键词
[编辑] 示例
演示 nullptr 保持空指针常量的含义,即使它不再是字面量。
#include <cstddef> #include <iostream> template<class T> constexpr T clone(const T& t) { return t; } void g(int*) { std::cout << "函数 g 已调用\n"; } int main() { g(nullptr); // 良好 g(NULL); // 良好 g(0); // 良好 g(clone(nullptr)); // 良好 // g(clone(NULL)); // 错误:非字面量的零不能为空指针常量 // g(clone(0)); // 错误:非字面量的零不能为空指针常量 }
输出:
函数 g 已调用 函数 g 已调用 函数 g 已调用 函数 g 已调用
[编辑] 引用
- C++23 标准(ISO/IEC 14882:2024):
- 7.3.12 Pointer conversions [conv.ptr]
- C++20 标准(ISO/IEC 14882:2020):
- 7.3.12 Pointer conversions [conv.ptr]
- C++17 标准(ISO/IEC 14882:2017):
- 7.11 Pointer conversions [conv.ptr]
- C++14 标准(ISO/IEC 14882:2014):
- 4.10 Pointer conversions [conv.ptr]
- C++11 标准(ISO/IEC 14882:2011):
- 4.10 Pointer conversions [conv.ptr]
[编辑] 参阅
| 实现定义的空指针常量 (宏常量) | |
| (C++11) |
空指针字面量 nullptr 的类型 (typedef) |
| nullptr 的 C 文档
| |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
