Вопрос

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)

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top