【C++11】新特性——引入nullprt
2015-12-18 20:16
471 查看
原文地址:http://blog.csdn.net/huang_xw/article/details/8764346
[cpp]
view plaincopyprint?
#define NULL (void*)0
但同时,也允许将NULL定义为整常数0
[cpp]
view plaincopyprint?
// lmcons.h中定义NULL的源码
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
[cpp]
view plaincopyprint?
// 考虑下面两个重载函数
void foo(int i);
void foo(char* p)
foo(NULL); // which is called?
对于编译器,Visual Studio 2010已经开始支持C++0x中的大部分特性,自然包括nullptr。而VS2010之前的版本,都不支持此关键字。
Codeblocks10.5附带的G++ 4.4.1不支持nullptr,升级为4.6.1后可支持nullptr(需开启-std=c++0x编译选项)
[cpp]
view plaincopyprint?
int* p1 = 0;
int* p2 = nullptr;
if(p1 == 0) {}
if(p2 == 0) {}
if(p1 == nullptr) {}
if(p2 == nullptr) {}
if(p1 == p2) {}
if(p2) {}
不能将nullptr赋值给整形,如下示例:
[cpp]
view plaincopyprint?
int n1 = 0; // ok
int n2 = nullptr; // error
if(n1 == nullptr) {} // error
if(n2 == nullptr) {} // error
if(nullprt) {} // error
nullptr = 0 // error
上面提到的重载问题,使用nullptr时,将调用char*。
[cpp]
view plaincopyprint?
void foo(int) {cout << "int" << endl;}
void foo(char*) {cout << "pointer" << endl;}
foo(0); // calls foo(int)
foo(nullptr); // calls foo(char*)
[cpp]
view plaincopyprint?
const
class nullptr_t_t
{
public:
template<class T> operator T*() const {return 0;}
template<class C, class T> operator T C::*() const { return 0; }
private:
void operator& () const;
} nullptr_t = {};
#undef NULL
#define NULL nullptr_t
1. 引入nullptr的原因
引入nullptr的原因,这个要从NULL说起。对于C和C++程序员来说,一定不会对NULL感到陌生。但是C和C++中的NULL却不等价。NULL表示指针不指向任何对象,但是问题在于,NULL不是关键字,而只是一个宏定义(macro)。1.1 NULL在C中的定义
在C中,习惯将NULL定义为void*指针值0:[cpp]
view plaincopyprint?
#define NULL (void*)0
但同时,也允许将NULL定义为整常数0
1.2 NULL在C++中的定义
在C++中,NULL却被明确定义为整常数0:[cpp]
view plaincopyprint?
// lmcons.h中定义NULL的源码
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
1.3为什么C++在NULL上选择不完全兼容C?
根本原因和C++的重载函数有关。C++通过搜索匹配参数的机制,试图找到最佳匹配(best-match)的函数,而如果继续支持void*的隐式类型转换,则会带来语义二义性(syntax ambiguous)的问题。[cpp]
view plaincopyprint?
// 考虑下面两个重载函数
void foo(int i);
void foo(char* p)
foo(NULL); // which is called?
2. nullptr的应用场景
2.1 编译器
如果我们的编译器是支持nullptr的话,那么我们应该直接使用nullptr来替代NULL的宏定义。正常使用过程中他们是完全等价的。对于编译器,Visual Studio 2010已经开始支持C++0x中的大部分特性,自然包括nullptr。而VS2010之前的版本,都不支持此关键字。
Codeblocks10.5附带的G++ 4.4.1不支持nullptr,升级为4.6.1后可支持nullptr(需开启-std=c++0x编译选项)
2.2 使用方法
0(NULL)和nullptr可以交换使用,如下示例:[cpp]
view plaincopyprint?
int* p1 = 0;
int* p2 = nullptr;
if(p1 == 0) {}
if(p2 == 0) {}
if(p1 == nullptr) {}
if(p2 == nullptr) {}
if(p1 == p2) {}
if(p2) {}
不能将nullptr赋值给整形,如下示例:
[cpp]
view plaincopyprint?
int n1 = 0; // ok
int n2 = nullptr; // error
if(n1 == nullptr) {} // error
if(n2 == nullptr) {} // error
if(nullprt) {} // error
nullptr = 0 // error
上面提到的重载问题,使用nullptr时,将调用char*。
[cpp]
view plaincopyprint?
void foo(int) {cout << "int" << endl;}
void foo(char*) {cout << "pointer" << endl;}
foo(0); // calls foo(int)
foo(nullptr); // calls foo(char*)
3. 模拟nullptr的实现
某些编译器不支持c++11的新关键字nullptr,我们也可以模拟实现一个nullptr。[cpp]
view plaincopyprint?
const
class nullptr_t_t
{
public:
template<class T> operator T*() const {return 0;}
template<class C, class T> operator T C::*() const { return 0; }
private:
void operator& () const;
} nullptr_t = {};
#undef NULL
#define NULL nullptr_t
相关文章推荐
- Linux C函数参考手册(PDF版)
- Lua教程(十七):C API简介
- C#实现打造气泡屏幕保护效果
- 使用C#代码获取存储过程返回值
- C/C++数据对齐详细解析
- 利用C语言来求最大连续子序列乘积的方法
- 字符串的组合算法问题的C语言实现攻略
- C 语言基础教程(我的C之旅开始了)[三]
- C++中的extern “C”用法详解
- C 语言基础教程(我的C之旅开始了)[七]
- 最大子矩阵问题实例解析
- C字符串操作函数实现方法小结
- C语言中static的作用及C语言中使用静态函数有何好处
- 字典树的基本知识及使用C语言的相关实现
- 详解C语言中free()函数与getpagesize()函数的使用
- C/C++ 宏详细解析
- 使用C语言解决字符串全排列问题
- C#定制Excel界面并实现与数据库交互的方法
- C#使用ImitateLogin模拟登录百度