1. 引入nullptr的原因

引入nullptr的原因,这个要从NULL说起。对于C和C++程序员来说,一定不会对NULL感到陌生。但是C和C++中的NULL却不等价。NULL表示指针不指向任何对象,但是问题在于,NULL不是关键字,而只是一个宏定义(macro)。

1.1 NULL在C中的定义

在C中,习惯将NULL定义为void*指针值0:

  1. #define NULL (void*)0

但同时,也允许将NULL定义为整常数0

1.2 NULL在C++中的定义

在C++中,NULL却被明确定义为整常数0:

  1. // lmcons.h中定义NULL的源码
  2. #ifndef NULL
  3. #ifdef __cplusplus
  4. #define NULL    0
  5. #else
  6. #define NULL    ((void *)0)
  7. #endif
  8. #endif

1.3为什么C++在NULL上选择不完全兼容C?

根本原因和C++的重载函数有关。C++通过搜索匹配参数的机制,试图找到最佳匹配(best-match)的函数,而如果继续支持void*的隐式类型转换,则会带来语义二义性(syntax ambiguous)的问题。

C语言没函数重载(c++重载主要依靠名字粉碎)。c语言可以通过可变参数或函数指针实现重载。http://www.cnblogs.com/haippy/archive/2012/12/27/2835358.html

http://www.linuxidc.com/Linux/2012-07/66399.htm

  1. // 考虑下面两个重载函数
  2. void foo(int i);
  3. void foo(char* p)
  4. 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可以交换使用,如下示例:

int* p1 = ;
int* p2 = nullptr; if(p1 == ) {}
if(p2 == ) {}
if(p1 == nullptr) {}
if(p2 == nullptr) {}
if(p1 == p2) {}
if(p2) {}

不能将nullptr赋值给整形,如下示例:

  1. int n1 = 0;             // ok
  2. int n2 = nullptr;       // error
  3. if(n1 == nullptr) {}    // error
  4. if(n2 == nullptr) {}    // error
  5. if(nullprt) {}          // error
  6. nullptr = 0             // error

上面提到的重载问题,使用nullptr时,将调用char*。

  1. void foo(int)   {cout << "int" << endl;}
  2. void foo(char*) {cout << "pointer" << endl;}
  3. foo(0);       // calls foo(int)
  4. foo(nullptr); // calls foo(char*)

3. 模拟nullptr的实现

某些编译器不支持c++11的新关键字nullptr,我们也可以模拟实现一个nullptr。

const
class nullptr_t_t
{
public:
template<class T> operator T*() const {return ;}
template<class C, class T> operator T C::*() const { return ; }
private:
void operator& () const;
} nullptr_t = {};
#undef NULL
#define NULL nullptr_t

c++ 11nullptr的更多相关文章

  1. C++11---nullptr

    1.nullprt与NULL 代码: void f(int i) {    cout << "f(int)" << endl;} void f(char* ...

  2. C/C++ 随笔目录

    [1]基础部分 (1)宏定义 <assert> <offset宏> <#pragma once> <宏定义学习> <预处理语句> <# ...

随机推荐

  1. Fly (From Wikipedia)

    True flies are insects of the order Diptera, the name being derived from the Greek δι- di- "two ...

  2. git设置忽略文件和目录

    1.登录gitbash命令端进入本地git库目录 Administrator@PC201601200946 MINGW32 /d/gitrespository/crmweb (master) 2.创建 ...

  3. Android Canvas和Paint基本使用

    这篇文章主要介绍下画笔Paint和画布Canvas的基本使用  1.Paint 创建对象Paint mPaint = new Paint(); 常用的基本方法有 :                mP ...

  4. 使用StringTokenizer分解字符串

    Java切割字符串.一般使用substring.split.StringTokenizer来处理,前两种是String对象的方法,使用字符串能够直接处理,本文介绍下StringTokenizer的使用 ...

  5. mysq for visual studio 1.1.1

    https://cdn.mysql.com/Downloads/MySQLInstaller/mysql-visualstudio-plugin-1.1.1.msi

  6. shell基础(二)

    echo命令 Shell 的 echo 指令是用于字符串的输出. #!/bin/sh read name #读取标准输入的行 echo "$name It is a test" e ...

  7. JMeter java.net.URISyntaxException: Illegal character in query at index

    请求参数未编码,会造成请求解析失败.把编码勾上,就可以了.

  8. python 解析 XML文件

    如下使用xml.etree.ElementTree模块来解析XML文件.ElementTree模块中提供了两个类用来完成这个目的: ElementTree表示整个XML文件(一个树形结构) Eleme ...

  9. excel导出工具类

    package com.jianwu.util.excel; import com.google.common.collect.Lists;import com.jianwu.exception.Mo ...

  10. mac下面安装mysql

    参考http://www.cnblogs.com/lakeslove/p/6280404.html 关于msyql5.7,安装时最大的改变就是有了一个默认密码 我安装的是mysql-5.7.17-ma ...