如何定义同时支持C ++ 03和C ++ 11的nullptr? [英] How to define nullptr for supporting both C++03 and C++11?

查看:115
本文介绍了如何定义同时支持C ++ 03和C ++ 11的nullptr?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
反向移植"将nullptr转换为C ++-pre-C ++ 0x程序

Possible Duplicate:
“Backporting” nullptr to C++-pre-C++0x programs

如何定义nullptr以同时支持C ++ 03和C ++ 11?

How to define nullptr for supporting both C++03 and C++11?

下面的代码可以在不更改C ++ 11编译器中nullptr的含义的情况下使用C ++ 03和C ++ 11编译器编译吗?

Does below code is compiled with both C++03 and C++11 compiles without change the meaning of nullptr in C++11 compiler?

#include <cstddef>

#if !defined(nullptr)
#define nullptr NULL
#endif

推荐答案

在C ++ 11中,nullptr的类型为nullptr_t.与NULL(或者说0会说Bjarne因为他不喜欢宏)相比,它的一大优势是在这两个函数之间:

In C++11, nullptr is of type nullptr_t. One of its big advantages compared to NULL (or to 0 would say Bjarne since he does not like macros) is that between these two functions:

void foo(char*);
void foo(int);

foo(nullptr)将调用char*重载,但foo(NULL)将调用int重载.因此,您的解决方案在大多数情况下都可以使用,但是函数重载可能会导致错误.

foo(nullptr) will call the char* overload but foo(NULL) will call the int overload. So your solution may work in most of the cases, but you may have errors with functions overload.

这篇关于如何定义同时支持C ++ 03和C ++ 11的nullptr?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆