decltype 的行为
假设我有一些 stl 容器类 obj
的对象.我可以通过这种方式定义其他相同类型的对象:
decltype(obj) obj2;
但我不能以这种方式为容器声明迭代器:
decltype(obj)::iterator it = obj.begin();
为什么?我做错了什么吗?
解决方案根据最终的 C++0x 草案 (FDIS),您的代码格式良好.这是 Visual Studio 编译器尚未实现的后期更改.
与此同时,一种解决方法是使用 typedef:
typedef decltype(obj) obj_type;obj_type::iterator it = obj.begin();
相关章节和诗句是5.1.1/8:
<前>合格 ID:[...]nested-name-specifier 模板opt unqualified-id嵌套名称说明符:[...]声明类型说明符 ::声明类型说明符:decltype ( 表达式 )为了完整起见:
原始核心问题
措辞建议
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?
解决方案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.
In the meantime, a workaround is to use a typedef:
typedef decltype(obj) obj_type;
obj_type::iterator it = obj.begin();
EDIT: 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 )
And for completeness's sake:
The original core issue
Proposal for wording
相关文章