gpt4 book ai didi

c++ - 可以在 gcc 中模拟 nullptr 吗?

转载 作者:IT老高 更新时间:2023-10-28 13:58:14 26 4
gpt4 key购买 nike

我看到 nullptr 是在 Visual Studio 2010 中实现的。我喜欢这个概念并想尽快开始使用它;但是 GCC 还不支持它。我的代码需要在两者上运行(但不必与其他编译器一起编译)。

有没有办法“模仿”它?比如:

#define nullptr NULL

(显然这根本行不通,只是为了说明我的意思。)

最佳答案

The Official proposal有一个解决方法 -

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

关于c++ - 可以在 gcc 中模拟 nullptr 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2419800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com