2011-04-02 31 views
14

隨着一類,如美孚:的C++ 0x decltype和範圍解析操作

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; 

但我更喜歡保持名稱空間乾淨。我認爲優先級可能是一個問題,所以我也嘗試了括號,但沒有運氣。這是不可能的,還是有一種語法可以幫助我?

回答

13

它是有效的C++ 0x說decltype(f)::i。 GCC目前還不支持它。你可以用一個身份模板

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

identityboost::mpl命名空間的一部分工作,它周圍。

+0

Visual Studio 2010也存在這個問題。很好的解決方法。 – 2013-05-15 19:20:50