在查看 std::allocator 时,我发现以下成员已被弃用:
value_type
、
pointer
、
const_pointer
、
reference
、
const_reference
、
size_type
、
difference_type
和
rebind
。
分配器也不再拥有以下成员:
address
、
max_size
、
construct
或者 destroy
。
为什么会这样呢?这与多态分配器有关吗?
在查看 std::allocator 时,我发现以下成员已被弃用:
value_type
、
pointer
、
const_pointer
、
reference
、
const_reference
、
size_type
、
difference_type
和
rebind
。
分配器也不再拥有以下成员:
address
、
max_size
、
construct
或者 destroy
。
为什么会这样呢?这与多态分配器有关吗?
如果你看一下相关的isocpp论文,你会发现你提到的第一个集合现在被认为更适合放在std::allocator_traits
中。自STL(甚至标准库)发布以来,使用traits的趋势已经更加明显。
rebind
也是一种遗留物。当STL首次发布时,别名和模板模板参数不受支持。有了这些语言特性,rebind
似乎相当复杂。例如,正如你可以在这个问题的答案中看到的,第4版《C++程序设计语言》第34.4.1节第998页中评论默认分配器类中“传统”的重新绑定成员:
template<typename U>
struct rebind { using other = allocator<U>;};
template<typename U>
using other = allocator<U>;
std::addressof
可以替换address()
了。谁需要max_size呢?其余部分应该可以通过就地new/显式析构函数调用进行替换。 - Sam Varshavchikallocator_traits
,它将填补缺失的部分。 - Bo Persson