C++;11使用decltype(var)后跟内部类型“时出现编译器错误”;var"; 我使用Visual C++ 2010,这里是我的代码片段: std::set<int> s; decltype(s)::value_type param = 0;
我看到在g++4.7.2版本中,代码编译得很好。因此,这可能是MSV中的一个编译器错误。C++;11使用decltype(var)后跟内部类型“时出现编译器错误”;var"; 我使用Visual C++ 2010,这里是我的代码片段: std::set<int> s; decltype(s)::value_type param = 0;,c++,visual-studio-2010,c++11,compiler-errors,decltype,C++,Visual Studio 2010,C++11,Compiler Errors,Decltype,我看到在g++4.7.2版本中,代码编译得很好。因此,这可能是MSV中的一个编译器错误。 目前,您可以尝试以下技巧: #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) #endi
目前,您可以尝试以下技巧:
#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
免责声明:当然,有了这个技巧,在模板内部时,您可能必须使用
typename
。为此,您可以再使用一个宏,例如#define TDECLTYPE(VAR)typename DECLTYPE(VAR)
这是去年在Connect上出现的Visual Studio错误。是的
该问题旁边列出了一个与@iammillind有效相同的解决方法,只是它使用了
std::identity
,该方法在C++11发布前不久出于任何原因从
中删除。(只有一个模板参数是等效的;在某些情况下是相同的。)这是一个编译器bug@Potatoswatter:是的,应该这样做,特别是当发布在Connect上的解决方案暗示VS支持它时。我只希望提交解决方案的人记下它;
#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
DECLTYPE(s)::value_type param = 0;