C ++ 0x decltype和范围解析运算符

时间:2011-04-02 20:12:57

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

有一个类如Foo:

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

我发现GCC 4.5将拒绝以下

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

如果我使用中间typedef,它将起作用,例如:

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

但我更喜欢保持名称空间干净。我认为优先权可能是一个问题,所以我也试过括号,但没有运气。它是不可能的,或者是否有一种语法可以帮助我?

1 个答案:

答案 0 :(得分:13)

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

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

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

相关问题