NULL
From cppreference.net
C++
Utilities library
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Type support
| Basic types | |||||||||||||||||||||
| Fixed width integer types (C++11) | |||||||||||||||||||||
| Fixed width floating-point types (C++23) | |||||||||||||||||||||
|
|||||||||||||||||||||
| Numeric limits | |||||||||||||||||||||
| C numeric limits interface | |||||||||||||||||||||
| Runtime type information | |||||||||||||||||||||
|
|||||||||||||||||||||
|
定义于头文件
<clocale>
|
||
|
定义于头文件
<cstddef>
|
||
|
定义于头文件
<cstdio>
|
||
|
定义于头文件
<cstdlib>
|
||
|
定义于头文件
<cstring>
|
||
|
定义于头文件
<ctime>
|
||
|
定义于头文件
<cwchar>
|
||
|
#define NULL /* 由实现定义 */
|
||
宏
NULL
是一个实现定义的
空指针常量
。
目录 |
可能的实现
#define NULL 0 // since C++11 #define NULL nullptr |
注释
在C语言中,宏
NULL
可能具有
void
*
类型,但这在C++中是不允许的,因为空指针常量不能具有该类型。
示例
运行此代码
#include <cstddef> #include <iostream> #include <type_traits> #include <typeinfo> class S; int main() { int* p = NULL; int* p2 = static_cast<std::nullptr_t>(NULL); void(*f)(int) = NULL; int S::*mp = NULL; void(S::*mfp)(int) = NULL; auto nullvar = NULL; // 使用 gcc/clang 编译时可能触发警告 std::cout << "nullvar 的类型是 " << typeid(nullvar).name() << '\n'; if constexpr(std::is_same_v<decltype(NULL), std::nullptr_t>) std::cout << "NULL 使用 std::nullptr_t 类型实现\n"; else std::cout << "NULL 使用整型实现\n"; [](...){}(p, p2, f, mp, mfp); // < 抑制“未使用变量”警告 }
可能的输出:
The type of nullvar is long NULL implemented using an integral type
参见
| nullptr (C++11) | 指定空指针值的指针字面量 |
|
(C++11)
|
空指针字面量
nullptr
的类型
(typedef) |
|
C 文档
关于
NULL
|
|