假设我有一个stl容器类obj的对象。我可以这样定义相同类型的其他对象:
decltype(obj) obj2;但我不能以这种方式为容器声明迭代器:
decltype(obj)::iterator it = obj.begin();为什么?我做错了什么吗?
发布于 2011-05-24 03:15:55
这是因为语言被解析的方式。
decltype(obj)::iterator it = obj.begin();你想让它变成
(decltype(obj)::iterator) it;但实际上,它变成了
decltype(obj) (::iterator) it;我不得不承认,我也很惊讶地看到情况是这样的,因为我确定我以前做过这样的事情。但是,在这种情况下,您可以只使用auto,甚至可以使用decltype(obj.begin()),但除此之外,您还可以
typedef decltype(obj) objtype;
objtype::iterator it;发布于 2011-05-24 09:47:16
在VC++的解析器被修复以反映FDIS之前,另一个解决方法是使用std::identity<>元函数:
std::identity<decltype(obj)>::type::iterator it = obj.begin();https://stackoverflow.com/questions/6101728
复制相似问题