-
nullptr
2020-06-09 15:56:23文章目录1 nullptr 1 nullptr nullptr是C++11中新引入的关键字,主要用来表示控制针,数据类型为:std::nullptr_t。使用nullptr能够避免在整数和指针之间发生混淆。 首先看一下NULL和nullptr的类型: cout << ...文章目录
1 nullptr
nullptr是C++11中新引入的关键字,主要用来表示控制针,数据类型为:
std::nullptr_t
。使用nullptr能够避免在整数和指针之间发生混淆。首先看一下NULL和nullptr的类型:
cout << typeid(NULL).name() << endl; cout << typeid(nullptr).name() << endl;
运行结果如下:
可以看到NULL和nullptr实际上是不同的类型。不过,对于指针的初始化以及指针有关的NULL场合,我们尽量使用nullptr替代NULL。如果不替换,两者之间相互比较也是没有任何问题的:
int* p = null; if (p == nullptr) { cout << "p == nullptr" << endl; }
参考资料:
-
Replace NULL and Nullptr_t with nullptr and nullptr_t
2020-12-01 22:16:45<p>Replace NULL and Nullptr_t with nullptr and nullptr_t <h2>Release Management <ul><li>Affected package(s): all</li><li>Issue(s) solved (if any): fix #0000</li></ul>该提问来源于开源项目:CGAL/... -
NULL与 nullptr的理解
2020-03-26 14:21:19nullptr本来我都不知道nullptr,学习的时候遇到了。这里查了一下,记录。
NULL
在C语言中,空指针可以使用(void *)0;NULL实际在c的头文件里面宏定义为空指针
#define NULL ((void *)0)
但在C++语言中,由于对语法的类型检查更为严格,因而空指针的值就不能表示为(void *)0;
例如,空指针的值FILE *fp=(void *)0;//编译报错。即此时的 FILE *fp = NULL错误! FILE *fp = 0;//正确
所以至少自C++98开始,修改了NULL的宏定义:
#define NULL 0
但是却带来了新的问题
函数重载,调用不明
。
来看一个例子#include<iostream> using namespace std; void f(int *p){ cout<<"p is a pointer,its value is "<<*p<<endl; } void f(int n){ cout<<"n is a number"<<n<<endl; } int main(){ f(NULL);//重载函数调用是不明确的。NULL = 0; return 0; }
C++11开始,定义了空指针的常值为nullptr,解决了上述函数重载问题。
nullptr
nullptr是C++11语言标准用来表示空指针的常量值,
可以指派给任意类型的指针变量
。部分编译器将之视为一个关键字,例如Visual Studio,部分使用旧标准的C++编译器则未实现,使用会报错!需要自行定义或引入额外的头文件
nullptr的实现const class nullptr_t { public: template<class T> inline operator T*() const { return 0; } template<class C, class T> inline operator T C::*() const { return 0; } private: void operator&() const; } nullptr = {};
修改上面示例,我们就能编译通过了。
C++语言标准规定:值0或std::nullptr_t类型的纯右值是空指针常量(null pointer constant)。可以通过空指针转换(null pointer conversion)成为某个类型的空指针值(null pointer value)。
C++语言标准还规定,在实参个数多于形参个数时(即可变参数个数的函数调用,可用va_arg来访问),
多出来的实参如果是std::nullptr_t类型,则转化为(void *)0供函数访问
。std::nullptr_t定义在标准头文件中。但实际上在源程序中不包含该头文件仍可以正常使用nullptr_t类型与nullptr对象。
nullptr_t
nullptr_t是字面常量nullptr的数据类型。它是一种特殊的类型,并不是一种指针类型也不是指向任何种变量类型的指针类型。nullptr_t位于std名字空间中,且定义于头文件中。可透过is_null_pointer来检查对象是否为这种类型。nullptr除了解决函数重载问题,在C++模板中它还有更好的表现
//来源:公众号【编程珠玑】,https://www.yanbinghu.com #include<iostream> using namespace std; template<typename Type1,typename ptrType> void test(Type1 fun,ptrType ptr) { /*do something*/ fun(ptr); return; } void fun(int *val) { cout<<"fun"<<endl; } int main(void) { test(fun,NULL); return 0; }
编译报错了:
main.cpp:8:8: error: invalid conversion from ‘long int’ to ‘int*’ [-fpermissive] fun(ptr);
很显然NULL被推导为long int,而不是空指针,因而导致函数类型不匹配而报错。
但是如果我们用nullptr就不会有上面的问题。结论:表示空指针建议使用nullptr
-
C++ nullptr
2016-03-30 10:27:35c++ nullptr#include<iostream> using namespace std; void get(int a) { cout<<__func__<<"int"<<endl; } void get(int *a) { cout<<__func__<<"int*"<<endl; } int main() { int *p =NULL;//(void*)0; int *q = nullptr;//C++11的空指针定义 get(NULL); get(nullptr); // get((void*)0); }
-
nullptr关键字
2017-09-25 21:53:24除了NULL之外,C++11新标准中又引入了nullptr来声明一个“空指针”,这样,我们就有下面三种方法来获取一个“空指针”: 如下: int *p1 = NULL; // 需要引入cstdlib头文件 int *p2 = 0; int *p3 = nullptr; ...我们声明一个指针后最好马上对其进行初始化操作。如果暂时不明确该指针指向哪个变量,则需要赋予NULL值。除了NULL之外,C++11新标准中又引入了nullptr来声明一个“空指针”,这样,我们就有下面三种方法来获取一个“空指针”:
如下:
int *p1 = NULL; // 需要引入cstdlib头文件 int *p2 = 0; int *p3 = nullptr;
新标准中建议使用nullptr代替NULL来声明空指针。到这里,大家心里有没有疑问:为什么C++11要引入nullptr?它与NULL相比又有什么不同呢?这就是我们今天要解决的问题。
C/C++中的NULL到底是什么
我们查看一下C和C++的源码,不难发现:
1.NULL在C++中的定义,NULL在C++中被明确定义为整数0:
2.NULL在C中的定义.在C中,NULL通常被定义为如下:/* Define NULL pointer value */ #ifndef NULL #ifdef __cplusplus #define NULL 0 #else /* __cplusplus */ #define NULL ((void *)0) #endif /* __cplusplus */ #endif /* NULL */
#define NULL ((void *)0)
也就是说NULL实质上是一个void *指针。
那么问题又来了,我们从一开始学习C++的时候就被告诫C++是兼容C的,为什么对于NULLC++却不完全兼容C呢?通过查找维基百科,才发现这其中的原因。
简单地说,C++之所以做出这样的选择,根本原因和C++的函数重载机制有关。考虑下面这段代码:
void Func(char *); void Func(int); int main() { Func(NULL); }
如果C++让NULL也支持void *的隐式类型转换,这样编译器就不知道应该调用哪一个函数。
为什么要引入nullptr
C++把NULL定义为0,解决了函数重载后的函数匹配问题,但是又引入了另一个“问题”,同样是上面这段代码:
void Func(char *); void Func(int); int main() { Func(NULL); // 调用Func(int) }
由于我们经常使用NULL表示空指针,所以从程序员的角度来看,Func(NULL)应该调用的是Func(char *)但实际上NULL的值是0,所以调用了Func(int)。nullptr关键字真是为了解决这个问题而引入的。
另外我们还有注意到NULL只是一个宏定义,而nullptr是一个C++关键字。
nullptr如何使用
nullptr关键字用于标识空指针,是std::nullptr_t类型的(constexpr)变量。它可以转换成任何指针类型和bool布尔类型(主要是为了兼容普通指针可以作为条件判断语句的写法),但是不能被转换为整数。
char *p1 = nullptr; // 正确 int *p2 = nullptr; // 正确 bool b = nullptr; // 正确. if(b)判断为false int a = nullptr; // error
转载至:点击打开链接 -
nullptr详解
2016-12-15 11:04:501. 引入nullptr的原因 引入nullptr的原因,这个要从NULL说起。对于C和C++程序员来说,一定不会对NULL感到陌生。但是C和C++中的NULL却不等价。NULL表示指针不指向任何对象,但是问题在于,NULL不是关键字,而只是一... -
C++中nullptr
2020-11-25 20:56:46C++11中引入的关键字 nullprt也是代表空指针 ... char *q = nullptr; //int a = nullptr;//不可以 int b = NULL; if (p == nullptr) { cout << "p == nullptr" << endl; } if (q == NULL) { -
24nullptr
2018-07-08 01:39:48一、NULL与nullptr#include <iostream> using namespace std; void show(int num) { cout << "int" << endl; } void show(int *p) //函数重载 { cout &... -
走进C++11(十一) nullptr/nullptr_t
2020-11-21 16:50:38关键词 nullptr 代表指针字面量。它是 std::nullptr_t 类型的纯右值。存在从 nullptr 到任何指针类型及任何成员指针类型的隐式转换。同样的转换对于任何空指针常量也存在,空指针常量包括 std::nullptr_t 的值,以及... -
nullptr 简介
2016-10-07 11:17:431. 引入nullptr的原因 引入nullptr的原因,这个要从NULL说起。对于C和C++程序员来说,一定不会对NULL感到陌生。但是C和C++中的NULL却不等价。NULL表示指针不指向任何对象,但是问题在于,NULL不是关键字,而只是一... -
REFAC(client): replace NULL and Q_NULLPTR with nullptr
2021-01-09 12:33:49<div><p>This changes all occurances of NULL and Q_NULLPTR in the mumble source dir to nullptr. Additionally comparisons with NULL were changed to <code>if(pointer)</code> if it would not be confusing.... -
C++Primer 练习16.28 std::nullptr_t和nullptr
2020-08-29 20:29:42nullptr_t和nullptr 一. nullptr与nullptr_t (一)nullptr_t是一种数据类型,而nullptr是该类型的一个实例。通常情况下,也可以通过nullptr_t类型创建另一个新的实例。 (二)所有定义为nullptr_t类型的数据都是... -
Assertion failed: _pipe == nullptr & window == nullptr
2021-01-06 14:54:21nullptr at line 173 of panda/src/display/graphicsOutput.cxx Assertion failed: _window == nullptr at line 61 of panda/src/display/displayRegion.cxx Assertion failed: _window == nullptr... -
Replace NULL with nullptr
2021-01-09 10:48:46<p>We always used <code>NULL, for... Qt is replacing <code>Q_NULLPTR</code> (macro defined to either <code>nullptr</code> or <code>NULL, depending on the compiler) with <code>nullptr</code> everywhere: ... -
nullptr介绍
2016-03-31 23:06:34int *p1=nullptr; int *p2=NULL; cout return 0; } nullptr是C++11 新保准,相当于空指针,地址为零,和NULL相同效果。 需要注意的是能将0常量赋值给指针,却不能将int 变量赋值给指针。 -
Require support for nullptr
2020-12-07 04:13:48<div><p>HPX is already using nullptr, mostly in compute. There is a test for nullptr detection, but neither it is required nor a replacement macro is provided. <p>We decided to require nullptr for HPX... -
NULL和nullptr
2020-04-27 17:21:21我说一下我的理解, NULL和nullptr都可以当作是空指针。 但是,在c++中,两个做参数时。 NULL相当于0;但是nullptr确实是指针类型。 -
nullptr与NULL
2019-04-24 17:42:34nullptr详解:https://blog.csdn.net/u010983763/article/details/53667468 NULL与nullptr;https://blog.csdn.net/znzxc/article/details/80354434 用空指针就尽可能的使用nullptr。 -
C++自定义NULLPTR
2019-09-22 23:29:44惊奇的发现C++中连NULL和nullptr都有区别 NULL和nullptr 根据文章,应当做好NULL和nullptr的兼容工作 翻阅了一下qt的宏定义 #ifdef __GNC__ #define CC_GNU (__GNC__*100+__GNUC_MINOR__) #endif #...