【发布时间】:2013-04-27 22:12:35
【问题描述】:
所以我查看了 std::vector 的规范,并注意到 reference typedef 从 C++03 中的 Allocator::reference 更改为 C++11 中的 value_type&。我很惊讶,所以我开始深入研究。
在 C++03 §20.1.5 [lib.allocator.requirements] 中有表 32,其中 X::reference 定义为 T&,X::const_reference 定义为 T const&。
但是,在 C++11 §17.6.3.5 [allocator.requirements] 中有表 28,其中缺少 reference 和 const_reference。
接下来,我们在 C++11 中添加了 §20.6.8 std::allocator_traits,其中不包括 reference。但是§20.6.9 std::allocator 可以。
最后,有 §23.2.1 [container.requirements.general] 将 X::reference 定义为“T 的左值”,将 X::const_reference 定义为“T 的常量左值”。
所以,我在谷歌上搜索并找到了这篇论文 (1, 2),它建议从分配器要求中删除 reference,但它没有提及其背后的任何理由。但也有LWG issue反对改变。
另外,我发现 the interview with Alexander Stepanov 在其中谈到了 reference 如何封装特定于机器的内存布局,而在 Herb Sutter's post 中他谈到了获取指向容器元素的指针、容器要求以及 std::vector<bool> 是如何做到的一个容器。
那么,您对这一切有什么看法? reference 有用吗,是否达到了目的? “花哨”的参考文献如何符合标准?这是彻底消除它们、制定更严格的容器要求并弃用std::vector<bool> 的大胆举措吗?
【问题讨论】:
-
很好研究的问题
-
不,它不是很大胆。如果有人尝试实例化 std::vector
,我会破坏构建
标签: c++ memory-management c++11 allocator