如何使用decltype访问依赖类型?

Mor*_*hai 4 c++ visual-studio-2010 c++11

第一:如果我的命名错误,我道歉!

基本上,我有一个非常罕见的想要声明基于堆栈的容器,例如:

std::map<CString, size_t> ecounts;
Run Code Online (Sandbox Code Playgroud)

然后我想在函数体中进一步向下迭代ecounts的内容,但我真的不想要输入一些东西,也不需要重新输入上面的类型以便让编译器使用什么我有...

std::foreach(ecounts.begin(), ecounts.end(), [&] (>>>here is the problem<<< e)
{
  ... whatever I want to do with e ...
}
Run Code Online (Sandbox Code Playgroud)

当然,我可以使用typedef,也可以手动使用ecounts声明:

std::foreach(ecounts.begin(), ecounts.end(), [&] (std::pair<CString,size_t> e)
...
Run Code Online (Sandbox Code Playgroud)

但是,哎呀!我宁愿单独声明ecounts是什么,只是以某种方式使用它的value_type.但这似乎不起作用:

std::foreach(ecounts.begin(), ecounts.end(), [&] (decltype(ecounts)::value_type e)
...
Run Code Online (Sandbox Code Playgroud)

这只是我的编译器(vs2010)的限制,还是这是C++的限制?

我怎样才能为这样的代码制定一种单一定义规则,最好不必使用typedef来实现它(即,我可以执行以下操作):

typedef std::map<CString, size_t> maptype;
typedef maptype::value_type valuetype;
maptype ecounts;
...
std::foreach(ecounts.begin(), ecounts.end(), [&] (valuetype e)
...
Run Code Online (Sandbox Code Playgroud)

显然,这不是世界末日,但如果我可以使用decltype,我会因为减少思考和回溯来实现上述目标而感到高兴...

Xeo*_*Xeo 8

VS2010的局限性,因为您想要的添加进入标准为时已晚.它应该使用一致的编译器进行编译.作为一个workaroung,只需使用decltype(*ecounts.begin()) e.或身份模板:

template<class T>
struct identity{ typedef T type; };
// usage: identity<decltype(ecounts)>::type::value_type
Run Code Online (Sandbox Code Playgroud)