【问题标题】:C++0x decltype and the scope resolution operatorC++0x decltype 和范围解析运算符
【发布时间】:2011-07-28 09:21:42
【问题描述】:

使用诸如 Foo 之类的类:

struct Foo { static const int i = 9; };

我发现 GCC 4.5 会拒绝以下内容

Foo f;
int x = decltype(f)::i;

如果我使用中间类型定义,它将起作用,例如:

typedef decltype(f) ftype;
int x = ftype::i;

但我更喜欢保持命名空间干净。我认为优先级可能是一个问题,所以我也尝试了括号,但没有运气。是不可能的,还是有什么语法可以帮助我?

【问题讨论】:

    标签: c++ c++11 typeof decltype scope-resolution


    【解决方案1】:

    decltype(f)::i 是有效的 C++0x。 GCC 只是还不支持它。您可以使用身份模板解决它

    template<typename T> struct identity { typedef T type; };
    int x = identity<decltype(f)>::type::i;
    

    identityboost::mpl 命名空间的一部分。

    【讨论】:

    • Visual Studio 2010 也存在这个问题。很好的解决方法。
    猜你喜欢
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 2021-09-26
    • 2012-04-20
    • 2016-08-15
    • 2011-05-19
    • 2013-06-13
    相关资源
    最近更新 更多