转载: http://blog.csdn.net/soal IAP/article/details/7663865
1972年c语言刚刚诞生,常量0同时充当整数(int ) 0和空指针。 为了避免理解上的二义性,c语言通常将空宏用作空指针,空宏通常定义为(void * ) 0或0,而c仅将空指针用作0。 因此,例如,对于重载函数fun(char* )和fun (int )的调用,直接使用NULL作为参数调用fun会有问题。NULL调用fun(char* )函数作为NULL指针的表示nullptr关键字是为了解决这个问题而产生的。
nullptr可以转换为任何指针类型和bool类型,包括成员函数指针和成员变量指针。 这是一种与常规指针兼容的格式,使您可以使用ptr确定指针是否为空指针。 但是,不能转换为整数0。
虽然分析并测试了nullptr_t在libcxx上的实现,但对于将其转换为bool类型的调用仍然感到困惑。
//test.CPP # includeiostreamusingstd :3360 cout; using std:endl; namespacemyname { struct nullptr _ t { void * _; struct __nat {int __for_bool_; (; nullptr_t(int_NAT:* ) /构造函数,参数为0 (空指针)或_ NAT :3360 _ for _ bool _ operatorint/*或返回假; //为什么不这样做? template class _ TP operator _ TP * (常数{ return 0; //非成员变量指针、指向非成员函数指针的常规指针template class _Tp、class _ up operator _ TP _ up :3360 * (const { return 0; //成员变量指针或成员函数指针friend bool operator==(nullptr_t,nullptr_t ) {return true; } friend bool操作器!=(nullptr_t,=(nullptr_t ) { return false; }friendbooloperator(nullptr_t,nullptr_t ) {return false; }friendbooloperator=(nullptr_t,nullptr_t ) {return true; }friendbooloperator(nullptr_t,nullptr_t ) {return false; }friendbooloperator=(nullptr_t,nullptr_t ) {return true; }; 结构测试{ }; }using namespace myname; inline nullptr_t __get_nullptr_t () {return nullptr_t(0)0}; }#define nullptr __get_nullptr_t () intmain ) intargc,char *argv[] () {char *pch=nullptr; //ok,_ get _ nullptr _ t (=nullptr _ t :3360 nullptr _ t )0)=template class _ TP nullptr _ t 3365365374; t/ook _ get _ nullptr _ t (=nullptr _ t :3360 nullptr _ t )0)=template class _ TP nullptr _ t 33603360 ook///ok,_ gege //error : cannot convert ' my name :3360 nullptr _ t ' to ' int ' initializationinttest 3360: * p=nullptr; //ok,_ get _ nullptr _ t (=nullptr _ t :3360 nullptr _ t )0)=template class _Tp,class_poperator //ok _ get _ nullptr _ t (=nullptr _ t :3360 nullptr _ t )0)=template class _Tp,class_poperator }参考:维基百科c11