Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 330
Borgleader Avatar asked Jul 06 '13 03:07

Borgleader


People also ask

What is the purpose of scope resolution?

The scope resolution operator :: is used to identify and disambiguate identifiers used in different scopes. For more information about scope, see Scope.

What is the major application of scope resolution operator(:: in the classes?

The scope resolution operator ( :: ) is used for several reasons. For example: If the global variable name is same as local variable name, the scope resolution operator will be used to call the global variable. It is also used to define a function outside the class and used to access the static variables of class.

What does scope resolution operator do in c++?

The declaration of count declared in the main function hides the integer named count declared in global namespace scope. The statement ::count = 1 accesses the variable named count declared in global namespace scope.


1 Answers

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.

like image 110
James McNellis Avatar answered Nov 04 '22 06:11

James McNellis