【问题标题】:Why do standard containers require allocator_type::value_type to be the element type?为什么标准容器要求 allocator_type::value_type 为元素类型?
【发布时间】:2018-05-09 03:53:43
【问题描述】:

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

对于std::vectorstd::list(强调我的)都可以找到关于模板参数Allocator 的以下描述:

一个分配器,用于获取/释放内存并 构造/销毁该内存中的元素。类型必须满足 分配器的要求。 如果行为未定义 Allocator::value_typeT 不一样

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

【问题讨论】:

  • 我实际上在标准中找不到这个要求。我要么错过它,要么这里的 cpp 引用不正确。
  • @SergeyA 这在 [container.requirements.general] 中的“分配器感知容器要求”表中间接给出,allocator_type 表达式的断言/注释。
  • @1201ProgramAlarm,哦,一般容器要求!谢谢。
  • 嗯,我想还有一件更疯狂的事情要添加到关于分配器的列表中。这特别奇怪,因为std::list 几乎肯定会重新绑定到内部节点类型

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-02
    • 2017-04-07
    • 2017-07-17
    • 1970-01-01
    • 2023-01-11
    • 2011-12-13
    • 1970-01-01
    • 2011-05-31
    相关资源
    最近更新 更多