可以在gcc中模拟nullptr吗? [英] Can nullptr be emulated in gcc?

查看:110
本文介绍了可以在gcc中模拟nullptr吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I saw that nullptr was implemented in Visual Studio 2010. I like the concept and want to start using it as soon as possible; however GCC does not support it yet. My code needs to run on both (but doesn't have to compile with other compilers).

有没有办法模拟它?像:

Is there a way to "emulate" it? Something like:

#define nullptr NULL

(这显然不会很好,只是为了显示我的意思。)

(That obviously wouldn't work well at all, it's just to show what I mean.)

推荐答案

官方提案有一个解决方法 -

The Official proposal has a workaround -

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

这篇关于可以在gcc中模拟nullptr吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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