NULL vs nullptr (Why was it replaced?)(NULL 与 nullptr(为什么被替换?))
问题描述
我知道在 C++ 中 0x 或 NULL 在基于指针的应用程序中被 nullptr 替换.我只是想知道他们进行此替换的确切原因?
I know that in C++ 0x or NULL was replaced by nullptr in pointer-based applications. I'm just curious of the exact reason why they made this replacement?
在什么情况下使用 nullptr 而不是 NULL 在处理指针时有益?
In what scenario is using nullptr over NULL beneficial when dealing with pointers?
推荐答案
nullptr 具有类型 std::nullptr_t.它可以隐式转换为任何指针类型.因此,它将匹配重载解析中的 std::nullptr_t 或指针类型,但不匹配其他类型,例如 int.
nullptr has type std::nullptr_t. It's implicitly convertible to any pointer type. Thus, it'll match std::nullptr_t or pointer types in overload resolution, but not other types such as int.
0(又名.C 的 NULL 桥接到 C++)可能导致重载函数解析中的歧义,其中包括:
0 (aka. C's NULL bridged over into C++) could cause ambiguity in overloaded function resolution, among other things:
f(int);
f(foo *);
(感谢 Caleth 在评论中指出这一点.)
(Thanks to Caleth pointing this out in the comments.)
这篇关于NULL 与 nullptr(为什么被替换?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:NULL 与 nullptr(为什么被替换?)
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
