Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

c++ - Why does scope resolution fail in presence of decltype?

It is my understanding that decltype is used to query the type of an objects/variables and so on.

From the examples present on wikipedia, such as the following:

int i;
decltype(i) x3; // type is int

I assumed I could do something like this:

class A
{
public:
    int a, b;
};

template<typename T>
struct IsClass
{
    enum { Yes = std::is_class<T>::value };
    enum { No = !Yes };
};

std::vector<A> v;
auto it = v.begin();
IsClass<decltype(it)::value_type>::Yes

Because after all this line is legal:

IsClass<std::vector<A>::iterator::value_type>::Yes

Alas it wouldn't compile, citing the following: error C2039: 'value_type' : is not a member of 'global namespace''`

Any ideas as to why scope resolution was made to behave this way in presence of decltype?

P.S: If it makes any difference I'm using MSVC2012 (without the Nov CTP)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a known bug in the Visual C++ compiler. It has not yet been fixed as of the Visual C++ 2013 Preview. You can work around this issue using std::common_type:

IsClass<std::common_type<decltype(it)>::type::value_type>::Yes
        ^^^^^^^^^^^^^^^^^            ^^^^^^^

(std::common_type with a single template argument yields that argument type; it's the standardized C++11 equivalent of the identity template that has long been used in metaprogramming.)

You can find the public bug report on Microsoft Connect: Cannot use decltype before scope operator. If this issue is important to you, please consider upvoting that bug report.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...