decltype的行为 [英] Behavior of decltype
问题描述
说我有一些stl容器类的对象 obj
。我可以通过这种方式定义相同类型的其他对象:
Say I have an object of some of stl container classes obj
. I can define other object of same type this way:
decltype(obj) obj2;
但我不能用这种方式为容器声明迭代器:
But I can't declare iterator for the container this way:
decltype(obj)::iterator it = obj.begin();
为什么?我做错了什么?
Why? Am I doing something wrong?
推荐答案
您的代码根据最终的C ++ 0x草案(FDIS)格式良好。这是一个延迟的更改,还没有被Visual Studio编译器实现。
Your code is well-formed according to the final C++0x draft (FDIS). This was a late change that's not yet been implemented by the Visual Studio compiler.
同时,一个解决方法是使用typedef:
In the meantime, a workaround is to use a typedef:
typedef decltype(obj) obj_type;
obj_type::iterator it = obj.begin();
编辑:相关章节是5.1.1 /
The relevant chapter and verse is 5.1.1/8:
qualified-id:
[...]
nested-name-specifier templateopt unqualified-id
nested-name-specifier:
[...]
decltype-specifier ::
decltype-specifier:
decltype ( expression )
为了完整起见:
a href =http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3237.html#743>原始核心问题
The original core issue
这篇关于decltype的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!