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

旧巷老猫 提交于 2019-12-07 12:14:00

问题


Related: Deprecation of std::allocator<void>.

The following description about template parameter Allocator is found for both std::vector and std::list (emphasis mine):

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.

The last sentence does not make sense to me. If a specific value_type is required, couldn't it just do an allocator rebind?


回答1:


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.



来源:https://stackoverflow.com/questions/50245277/why-do-standard-containers-require-allocator-typevalue-type-to-be-the-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!