使用decltype(var)后跟内部类型"var"时出现C++ 11编译器错误

Tri*_*ant 8 c++ compiler-errors visual-studio-2010 decltype c++11

我正在使用Visual C++ 2010,这是我的代码片段:

std::set<int> s;
decltype(s)::value_type param = 0;
Run Code Online (Sandbox Code Playgroud)

我收到以下错误消息,任何人都可以帮助我?

> error C2039: 'value_type' : is not a member of '`global namespace''
> error C2146: syntax error : missing ';' before identifier 'param'
Run Code Online (Sandbox Code Playgroud)

Lig*_*ica 7

这是去年在Connect上提出的Visual Studio错误.这是问题757545("范围操作之前无法使用decltype") .

这个问题有一个与它一起列出的解决方法实际上与@ iammillind相同,除了它使用的std::identity<functional>在C++ 11发布之前不久被删除的,无论出于何种原因.(std::common_type一个模板参数是等效的; std::remove_reference在某些情况下是相同的.)

  • `std :: common_type <T>`实际上是正确的替换. (4认同)

iam*_*ind 5

我发现使用 g++ 4.7.2 版本,代码编译得很好。所以这可能是MSVS 中的编译器错误
暂时你可以尝试以下技巧:

#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
Run Code Online (Sandbox Code Playgroud)

将其用作:

DECLTYPE(s)::value_type param = 0;
Run Code Online (Sandbox Code Playgroud)

免责声明typename:当然,有了这个技巧,您可能必须在模板内使用。为此,您可以再添加 1 个宏,例如#define TDECLTYPE(VAR) typename DECLTYPE(VAR)