Mih*_*yan 11 c++ visual-studio-2010 decltype visual-c++ c++11
假设我有一些stl容器类的对象obj.我可以这样定义同类型的其他对象:
decltype(obj) obj2;
Run Code Online (Sandbox Code Playgroud)
但我无法以这种方式为容器声明迭代器:
decltype(obj)::iterator it = obj.begin();
Run Code Online (Sandbox Code Playgroud)
为什么?难道我做错了什么?
Joh*_*esD 16
根据最终的C++ 0x草案(FDIS),您的代码格式正确.这是Visual Studio编译器尚未实现的后期更改.
在此期间,解决方法是使用typedef:
typedef decltype(obj) obj_type;
obj_type::iterator it = obj.begin();
Run Code Online (Sandbox Code Playgroud)
编辑:相关章节和经文是5.1.1/8:
qualified-id:
[...]
nested-name-specifier templateopt unqualified-id
nested-name-specifier:
[...]
decltype-specifier ::
decltype-specifier:
decltype ( expression )
为了完整起见:
这是因为解析语言的方式.
decltype(obj)::iterator it = obj.begin();
Run Code Online (Sandbox Code Playgroud)
你希望它成为
(decltype(obj)::iterator) it;
Run Code Online (Sandbox Code Playgroud)
但事实上,它变成了
decltype(obj) (::iterator) it;
Run Code Online (Sandbox Code Playgroud)
我不得不承认,我也很惊讶地发现情况确实如此,因为我确信我之前已经这样做了.但是,在这种情况下,您可以使用auto,甚至可以decltype(obj.begin()),但此外,您可以这样做
typedef decltype(obj) objtype;
objtype::iterator it;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2777 次 |
| 最近记录: |