C++11 compiler error when using decltype(var) followed by internal type of "var"

I'm using Visual C++ 2010, and here's my code snippet:

std::set<int> s;
decltype(s)::value_type param = 0;

I got the following error message, anyone can help me?

> error C2039: 'value_type' : is not a member of '`global namespace''
> error C2146: syntax error : missing ';' before identifier 'param'

This is a Visual Studio bug that was raised last year on Connect. It is issue 757545 ("Cannot use decltype before scope operator").

The issue has a workaround listed alongside it that is effectively the same as @iammillind's, except it uses std::identity that was removed from <functional> shortly prior to the publication of C++11, for whatever reason. ( std::common_type with one template parameter is equivalent; std::remove_reference is the same in some cases.)


I see that with g++ 4.7.2 version, the code compiles fine. So it could be a compiler bug in MSVS.
For time being you can try below trick:

#ifdef COMPILER_BUG_STILL_THERE
template<typename T> struct Get { typedef T type; };
#define DECLTYPE(VAR) Get<decltype(VAR)>::type
#else
#define DECLTYPE(VAR) decltype(VAR)
#endif

Use it as:

DECLTYPE(s)::value_type param = 0;

Disclaimer: Ofcourse with this trick, you may have to use typename when inside templates. For that you can have 1 more macro such as #define TDECLTYPE(VAR) typename DECLTYPE(VAR)

链接地址: http://www.djcxy.com/p/66500.html

上一篇: 专用具有decltype追踪返回类型的函数模板

下一篇: 使用decltype(var)后跟内部类型“var”的C ++ 11编译器错误