gpt4 book ai didi

c++ - 为什么 std::allocator 在 C++17 中丢失了成员类型/函数?

转载 作者:IT老高 更新时间:2023-10-28 21:53:08 25 4
gpt4 key购买 nike

在查看 std::allocator 时,我看到成员(member):
value_type,指针,const_pointer,引用,const_reference,size_type,difference_type,以及rebind 已全部弃用。

分配器也将不再拥有成员:
addressmax_sizeconstructdestroy

为什么会这样?是否与多态分配器有关?

最佳答案

如果您查看 the relevant isocpp paper你可以看到你提到的第一个集合现在被认为放在 std::allocator_traits 中更好。 .自从 STL(甚至不是标准库)问世以来,使用 trait 发生了更多的转变。

rebind 也是遗物。 STL 刚出来时,不支持别名和模板模板参数。有了这些语言特性,rebind 似乎相当复杂。例如,您可以在 an answer to this question 中看到,在 C++ 编程语言,第 4 版,第 34.4.1 节,p。 998,评论默认分配器类中的“经典”重新绑定(bind)成员:

template<typename U>
struct rebind { using other = allocator<U>;};

Bjarne Stroustupr 写道:“奇怪的重新绑定(bind)模板是一个古老的别名。它应该是:

template<typename U>
using other = allocator<U>;

但是,分配器是在 C++ 支持此类别名之前定义的。"

因此,总的来说,标准库是在 catch 语言和范式的转变。

关于c++ - 为什么 std::allocator 在 C++17 中丢失了成员类型/函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38565365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com