为什么标准容器要求allocator_type :: value_type为元素类型? [英] Why do standard containers require allocator_type::value_type to be the element type?
问题描述
相关:弃用std :: allocator< void> .
对于 std::vector
和 std::list
(重点是我):
The following description about template parameter Allocator
is found for both std::vector
and std::list
(emphasis mine):
分配器,用于获取/释放内存并用于 构造/销毁该内存中的元素.类型必须符合 分配器的要求. 如果未定义行为,则为
Allocator::value_type
与T
不同.
An allocator that is used to acquire/release memory and to construct/destroy the elements in that memory. The type must meet the requirements of Allocator. The behavior is undefined if
Allocator::value_type
is not the same asT
.
最后一句话对我来说没有意义.如果需要特定的value_type
,难道不是只是重新分配分配器?
The last sentence does not make sense to me. If a specific value_type
is required, couldn't it just do an allocator rebind?
推荐答案
原因主要是历史性的-在C ++ 11添加allocator_traits
之前,重新绑定更为复杂. 网络TS 定义了"proto-allocator"的概念([async.reqmts.proto.allocator]),其中绑定总是在使用之前进行,因此似乎有一天该要求会放宽.
The reason is mostly historical—rebinding was more complicated before C++11 added allocator_traits
. The Networking TS defines a "proto-allocator" concept ([async.reqmts.proto.allocator]) where rebinding is always applied before any use, so it seems likely that the requirement will be relaxed someday.
这篇关于为什么标准容器要求allocator_type :: value_type为元素类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!