std:: nullptr_t
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Basic types | |||||||||||||||||||||
| Fixed width integer types (C++11) | |||||||||||||||||||||
| Fixed width floating-point types (C++23) | |||||||||||||||||||||
|
|||||||||||||||||||||
| Numeric limits | |||||||||||||||||||||
| C numeric limits interface | |||||||||||||||||||||
| Runtime type information | |||||||||||||||||||||
|
|||||||||||||||||||||
|
定义于头文件
<cstddef>
|
||
|
using
nullptr_t
=
decltype
(
nullptr
)
;
|
(C++11 起) | |
std::nullptr_t
是空指针字面量
nullptr
的类型。它是一个独立类型,本身不是指针类型或成员指针类型。该类型的纯右值是
空指针常量
,可以
隐式转换
为任何指针类型和成员指针类型。
sizeof ( std :: nullptr_t ) 等于 sizeof ( void * ) 。
注释
C++标准要求
<stddef.h>
将
<cstddef>
的内容置于全局命名空间,因此当包含
<stddef.h>
时,要求
nullptr_t
在全局命名空间中可用。
nullptr_t
在 C23 之前不属于 C 语言标准。
未指定
std::nullptr_t
的声明是否在其他标准库头文件中可用。即使标准要求使用
std::nullptr_t
,实现也可以通过例如拼写
decltype
(
nullptr
)
来避免引入该名称。
示例
如果两个或更多重载接受不同的指针类型,则需要提供接受
std::nullptr_t
的重载以接受空指针参数。
#include <cstddef> #include <iostream> void f(int*) { std::cout << "Pointer to integer overload\n"; } void f(double*) { std::cout << "Pointer to double overload\n"; } void f(std::nullptr_t) { std::cout << "null pointer overload\n"; } int main() { int* pi{}; double* pd{}; f(pi); f(pd); f(nullptr); // would be ambiguous without void f(nullptr_t) // f(0); // ambiguous call: all three functions are candidates // f(NULL); // ambiguous if NULL is an integral null pointer constant // (as is the case in most implementations) }
输出:
Pointer to integer overload Pointer to double overload null pointer overload
参见
| nullptr (C++11) | 指定空指针值的指针字面量 |
|
实现定义的空指针常量
(宏常量) |
|
|
(C++11)
(
DR*
)
|
检查类型是否为
std::nullptr_t
(类模板) |
|
C 文档
关于
nullptr_t
|
|