问题
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 asT
.
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