Behavior of decltype(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
这篇关于decltype 的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:decltype 的行为


基础教程推荐
- 常量变量在标题中不起作用 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07