简体   繁体   English

为什么标准容器需要allocator_type :: value_type作为元素类型?

[英]Why do standard containers require allocator_type::value_type to be the element type?

Related: Deprecation of std::allocator<void> . 相关: std :: allocator <void>的弃用

The following description about template parameter Allocator is found for both std::vector and std::list (emphasis mine): 对于std::vectorstd::list (强调我的),可以找到关于模板参数Allocator的以下描述:

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 as T . 如果Allocator::value_typeT不同,则行为未定义

The last sentence does not make sense to me. 最后一句话对我没有意义。 If a specific value_type is required, couldn't it just do an allocator rebind? 如果需要特定的value_type ,那么它不能只重新分配一个分配器吗?

The reason is mostly historical—rebinding was more complicated before C++11 added allocator_traits . 原因主要是历史 - 重新绑定在C ++ 11添加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. Networking TS定义了一个“proto-allocator”概念([async.reqmts.proto.allocator]),其中重新绑定总是在任何使用之前应用,因此似乎有一天需要放宽该要求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 分配器::指针和分配器:: value_type *什么时候不同? - When do Allocator::pointer and Allocator::value_type* differ? 是否有更好的方法来区分可调整大小的容器而不是allocator_type的存在? - Is there a better way to distinguish resizable containers than presence of allocator_type? 为什么允许分配器覆盖其value_type的ctors和dtors? - Why is an allocator allowed to override the ctors and dtors of its value_type? 为什么 std::list 分配器由 value_type 参数化为默认值? - Why is an std::list allocator parameterised by the value_type as a default? 在STL容器中使用value_type有什么用? - What is the use of value_type in STL containers? 没有从“ std :: allocator”到“ const allocator_type”的转换 - no conversion from “std::allocator” to “const allocator_type” 从不兼容类型&#39;value_type&#39;(aka&#39;std :: __ 1 :: vector)分配给&#39;int *&#39; <int, std::__1::allocator<int> &gt;&#39;) - Assigning to 'int *' from incompatible type 'value_type' (aka 'std::__1::vector<int, std::__1::allocator<int> >') 如果标准容器元素类型和std :: allocator类型不同,这是错误的吗? - Is it wrong if the standard container element type and std::allocator type are different? unseq 执行策略是否要求迭代器的 value_type 为 Cpp17CopyAssignable? - Do the unseq execution policy require the iterators' value_type to be Cpp17CopyAssignable? 为什么向量容器中的迭代器类型与value_type *不匹配? - Why iterator type is mismatched with value_type * in vector container?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM