14

使用诸如 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;

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

4

1 回答 1

13

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

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

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

于 2011-04-02T20:21:09.937 回答