C++0x decltype and the scope resolution operator

2019-01-24 03:43发布

With a class such as Foo:

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

I find that GCC 4.5 will reject the following

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

It will work if I use an intermediate typedef, such as:

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

but I prefer to keep the namespace clean. I thought precedence may be an issue, so I've also tried parentheses, but no luck. Is it impossible as presented, or is there a piece of syntax that can help me?

1条回答
贪生不怕死
2楼-- · 2019-01-24 04:17

It is valid C++0x to say decltype(f)::i. GCC just doesn't support it yet. You can work it around with an identity template

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

identity is part of the boost::mpl namespace.

查看更多
登录 后发表回答