假设我有一些 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
这篇关于decltype 的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
读取输入文件,最快的方法?read input files, fastest way possible?(读取输入文件,最快的方法?)
在 C++ 中读取格式化输入的最简单方法?The easiest way to read formatted input in C++?(在 C++ 中读取格式化输入的最简单方法?)
从 .txt 文件读取到 C++ 中的二维数组Reading from .txt file into two dimensional array in c++(从 .txt 文件读取到 C++ 中的二维数组)
如何在 C++ 中模拟按键按下How to simulate a key press in C++(如何在 C++ 中模拟按键按下)
为什么在 cin.ignore() 之后没有 getline(cin, var) 读取Why doesn#39;t getline(cin, var) after cin.ignore() read the first character of the string?(为什么在 cin.ignore() 之后没有 getline(cin, var) 读取
scanf 格式输入的 cin 类比是什么?What is the cin analougus of scanf formatted input?(scanf 格式输入的 cin 类比是什么?)