问题描述
如何定义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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!