简体   繁体   English

为什么std :: allocator需要propagate_on_container_move_assignment为真?

[英]Why does std::allocator require propagate_on_container_move_assignment to be true?

According to current Standard (20.7.9), std::allocator has a member propagate_on_container_move_assignment which is set to true_type : 根据当前的标准(20.7.9), std::allocator有一个成员propagate_on_container_move_assignment ,设置为true_type

template class allocator 模板类分配器
{ {
public: 上市:
typedef size_t size_type; typedef size_t size_type;
typedef ptrdiff_t difference_type; typedef ptrdiff_t difference_type;
typedef T* pointer; typedef T *指针;
typedef const T* const_pointer; typedef const T * const_pointer;
typedef T& reference; typedef T&reference;
typedef const T& const_reference; typedef const T&const_reference;
typedef T value_type; typedef T value_type;
template struct rebind { typedef allocator other; template struct rebind {typedef allocator other; }; };
typedef true_type propagate_on_container_move_assignment; typedef true_type propagate_on_container_move_assignment;
typedef true_type is_always_equal; typedef true_type is_always_equal;
[...] [...]

std::allocator has no data members and always compared as equal with any other std::allocator . std::allocator没有数据成员,并且总是与任何其他std::allocator Is there any reason to move those default allocators on move assignment? 有没有理由在移动分配上移动这些默认分配器?

I'm answering relative to C++11, as you indicated in the tag: 正如你在标签中指出的那样,我正在回答C ++ 11的相关问题:

If the trait weren't true, then assignment operations would need to perform a runtime check on whether the allocators are equal. 如果特征不成立,则赋值操作需要对分配器是否相等执行运行时检查 Yes, of course the allocators would always be equal, but the code doesn't know that and would still have to perform the check, and thus you cannot offer a noexcept guarantee. 是的,当然分配器总是相同的,但是代码不知道并且仍然必须执行检查,因此您不能提供noexcept保证。 With POCMA = true, you can statically know that you will steal resources and thus won't throw. 如果POCMA = true,您可以静态地知道您将窃取资源,因此不会抛出。

C++14 made the std::allocator have POCMA = true (in LWG2103 ). C ++ 14使得std::allocator具有POCMA = true(在LWG2103中 )。 It was false in C++11. 在C ++ 11中它是错误的。

C++17 introduced the new trait is_always_equal (in N4258 ) to allow a non-throwing exception specification for operations even when POCMA is false. C ++ 17引入了新的特征is_always_equal (在N4258中 ),即使POCMA为假,也允许操作的非抛出异常规范。

(I think it's fair to say that the design of allocators has never quite been completed, and to this day nobody is entirely sure how they are supposed to work.) (我认为公平地说,分配器的设计从未完全完成,直到今天,没有人完全确定它们应该如何工作。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM