一、NULL和nullptr区别
- NULL是宏定义,nullptr是关键字。
#ifndef NULL#ifdef __cplusplus#define NULL 0#else#define NULL ((void *)0)#endif
#endif
- nullptr可以隐式转换为任意指针类型,而NULL需要显示转换
void func(char *)
{std::cout << "char * func" << std::endl;
}
void func(int)
{std::cout << "int func" << std::endl;
}int main()
{//NULL; nullptr;func(NULL); //"int func"func((char *)NULL); //"char * func"func(nullptr); //"char * func"}
- NULL可以进行算数运算,nullptr不行
std::cout << NULL + 1 << std::endl; //1//std::cout << nullptr + 1 << std::endl; //编译器报错
- 比较运算也有区别,nullptr可以与任意指针或nullptr_t类型以及0比较(非0整数不允许),而NULL可以与整数和指针比较。
int *a = new int(4);if (nullptr < a)std::cout << "nullptr < a" << std::endl;if (NULL < a)std::cout << "NULL < a" << std::endl;
二、NULL和nullptr相同点
- 两者都不能取地址,但是nullptr_t类型的变量可以取地址。
- 两者的大小相等,且与平台相关。
- 两者都能与指针和0的比较。
std::cout << sizeof(NULL)<< sizeof(nullptr)<<"\n"<<(0==NULL) << (0==nullptr)<<std::endl; //8,8,true,true
if (nullptr==0)std::cout << "nullptr\n"<<std::endl; //nullptr
三、nullptr与nullptr_t的特性
- nullptr_t类型是由nullptr获取的
typedef decltype(nullptr) nullptr_t;
- 所有nullptr_t类型的变量行为都与nullptr等价,行为也一样
- 都能隐式转换为任意指针
- 不能转为非指针类型,即使reinterpret_cast<nullptr_t>()也不行。(实际上可以转为bool)
- 不能进行算术运算,
- 能进行关系运算,支持nullptr_t类型和指针类型,以及0,其他整数都不行
- 是类型,所以模板时不支持推导成T *
using namespace std;template<typename T> void g(T* t) {}template<typename T> void h(T t) {}int main(){g(nullptr); // 编译失败, nullptr的类型是nullptr_t,而不是指针g((float*) nullptr); // 推导出T = floath(0); // 推导出T = inth(nullptr); // 推导出T = nullptr_th((float*)nullptr); // 推导出T = float*}// 编译选项:g++ 7-1-4.cpp -std=c++11