nuz*_*uzz 47 c++ gcc nullptr c++11
我看到它nullptr是在Visual Studio 2010中实现的.我喜欢这个概念,并希望尽快开始使用它; 但是GCC还不支持它.我的代码需要在两者上运行(但不必与其他编译器一起编译).
有没有办法"模仿"它?就像是:
#define nullptr NULL
Run Code Online (Sandbox Code Playgroud)
(这显然不会很好,只是为了表明我的意思.)
N 1*_*1.1 61
官方提案有一个解决方法 -
const // this is a const object...
class {
public:
template<class T> // convertible to any type
operator T*() const // of null non-member
{ return 0; } // pointer...
template<class C, class T> // or any type of null
operator T C::*() const // member pointer...
{ return 0; }
private:
void operator&() const; // whose address can't be taken
} nullptr = {}; // and whose name is nullptr
Run Code Online (Sandbox Code Playgroud)
此外,gcc(实际上是g ++)多年来一直有一个扩展__null.当nullptr提案出来时,这被视为行业实施经验.
__null扩展可以检测特殊情况并警告它们,例如意外地将NULL传递给bool参数,当它打算传递给指针参数时(对函数进行更改,忘记调整调用端).
当然这不便携.上面的模板解决方案是可移植的
它看起来是通过gcc 4.6.1(Ubuntu 11.11 oneiric),添加了nullptr.
我的hpp/cpp文件的快速,递归sed查找和替换对我来说很好:
find . -name "*.[hc]pp" | xargs sed -i 's/NULL/nullptr/g'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18681 次 |
| 最近记录: |