c++ - 为什么标准容器要求 allocator_type::value_type 是元素类型?

标签 c++ containers language-lawyer c++-standard-library allocator

相关:Deprecation of std::allocator<void> .

以下关于模板参数 Allocator 的描述可以在 std::vector 中找到和 std::list (强调我的):

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.

最后一句对我来说没有意义。如果需要特定的 value_type,难道它不能重新绑定(bind)分配器吗?

最佳答案

原因主要是历史原因——在 C++11 添加 allocator_traits 之前,重新绑定(bind)更加复杂。 Networking TS定义了一个“proto-allocator”概念([async.reqmts.proto.allocator]),其中在任何使用之前总是应用重新绑定(bind),因此似乎有一天这一要求可能会放宽。

关于c++ - 为什么标准容器要求 allocator_type::value_type 是元素类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50245277/

相关文章:

c++ - 在类内部或外部重载运算符有什么区别?

c++ - 如何从给定 vector 中检测所有可能的子集?

c++ - 指针运算是在分配的存储UB上吗?

c++ - 如何从 C++ 程序中的容器类中删除第一个元素?

python - Web REPL架构

c++ - C++17 中复制构造函数的继承

javascript - 为什么 ''(空字符串)会渗透所有字符串?

c++ - 图形专用模板类

c++ - 不区分大小写的操作

docker - 在 docker 容器中运行时找不到 xUnit 测试