在std :: map中使用decltype

时间:2014-04-22 14:01:54

标签: c++ c++11 decltype

考虑以下三个陈述:

std::map<int, std::string> foo;
std::map<int, std::string>::value_type;
decltype(foo)::value_type;

为什么最后一个合法?我认为decltype(foo)将是一个运算符,产生地图类型std::map<int, std::string>,我可以从中提取value_type

我正在使用MSVC2012。

3 个答案:

答案 0 :(得分:5)

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;

答案 1 :(得分:1)

它有效,你有其他错误,你必须给你的变量命名:

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;

答案 2 :(得分:1)

使用:

std::remove_reference<decltype(foo)>::type::value_type
相关问题