C++ 在std::map中使用decltype

C++ 在std::map中使用decltype,c++,c++11,decltype,C++,C++11,Decltype,考虑以下三种说法: std::map<int, std::string> foo; std::map<int, std::string>::value_type; decltype(foo)::value_type; std::map foo; std::map::value\u类型; decltype(foo)::值\类型; 为什么最后一个不合法?我认为decltype(foo)将是一个生成映射类型std::map的操作符,我可以从中提取值\u type 我正在使用

考虑以下三种说法:

std::map<int, std::string> foo;
std::map<int, std::string>::value_type;
decltype(foo)::value_type;
std::map foo;
std::map::value\u类型;
decltype(foo)::值\类型;
为什么最后一个不合法?我认为
decltype(foo)
将是一个生成映射类型
std::map
的操作符,我可以从中提取
值\u type


我正在使用MSVC2012。

GCC和Clang允许这种语法,您的编译器无法正确实现C++11

不过,您可以这样做:

std::map<int, std::string> foo;
std::map<int, std::string>::value_type;
using some_type = decltype(foo);
some_type::value_type;
std::map foo;
std::map::value\u类型;
使用某种类型=decltype(foo);
some_type::value_type;

它是有效的,如果有其他错误,则必须为变量指定名称:

std::map<int, std::string> foo;
std::map<int, std::string>::value_type nn_var;
decltype(foo)::value_type nn2_var;
typedef decltype(foo)::value_type value_type;
std::map foo;
std::map::value\u type nn\u var;
decltype(foo):值\类型nn2 \变量;
typedef decltype(foo)::值\类型值\类型;
使用:

std::remove\u reference::type::value\u type

您需要添加变量名或某种初始化:。我不这么认为。第二条语句编译正确。你说得对。您的代码实际上应该按原样编译。我认为这是你的编译器中的一个bug。我对VS2012也有同样的问题。它没有完全实现C++11。我认为VS2013解决了这个问题,尽管我不完全确定。至少在MSVC2012中不是这样:
decltype(foo)::value\u type t仍然给我一个编译错误(全局作用域没有::value_类型)我没有MSVS2012,但它可以与g++4.8:和clang:好的,忽略这个rise4fun示例-它给出了“无错误编译的文件!”即使有明显的插入错误@SlodgeMonster不幸的是,Visual Studio 2013也遇到了这个问题。@JonathanMee在VS2013更新4下确实为我编译了它,但intellisence将这些行标记为redThanke。很高兴知道这是一个编译器错误。
std::remove_reference<decltype(foo)>::type::value_type