Why does `std :: pmr :: polymorphic_allocator` not apply to container movement?

From http://en.cppreference.com/w/cpp/memory/polymorphic_allocator :

polymorphic_allocatorDoes not apply to container copy destination, move destination, or swap. As a result, the polymorphic_allocator-using container move assignment can cause an outlier and swap two polymorphic_allocator-using containers whose dispensers do not compare the same results in undefined behavior.

Why do I need this behavior? Not only does this seem to introduce unreasonable undefined swap behavior, but, more importantly for my purposes, it implies that std::pmr::vectorit is actually a type not assigned to move. I mean, it can be assigned to move, but it is almost guaranteed to be ineffective.

std::vector<int> v = {1, 2, 3};
std::vector<int> w;
w = std::move(v);  // nocopy, nothrow

std::pmr::monotonic_buffer_resource mr(1000);
std::pmr::vector<int> v( {1, 2, 3}, &mr );
std::pmr::vector<int> w;
w = std::move(v);  // yescopy, yesthrow

, . v mr, v mr. . , -, mr. , , , , ; . ( , .)


, /, , :

std::pmr::monotonic_buffer_resource mr(1000);
std::pmr::vector<int> v( {1, 2, 3}, &mr );
std::pmr::vector<int> w(v.get_allocator());
w = std::move(v);  // nocopy, nothrow
+7
2

allocator_traits<>::propagate_on_container_copy/move_assignment/swap - . , . , .

, . , PMR : .

:

std::pmr::monotonic_buffer_resource mr(1000);
std::pmr::vector<int> v( {1, 2, 3}, &mr );
std::pmr::vector<int> w;
auto a1 = w.get_allocator();
w = std::move(v);
assert(w.get_allocator() == a1);

++ , . ; ++.

, , . , / .

noexcept . PMR , , .

, . , .

+8

UPD: @cpplearner - - vector .

, pmr , ( ).

, , , .

, . ?

class X {
  std::pmr::vector<std::pmr::vector<Y>> table;
 public:
  /**/
  void new_data(std::pmr::vector<Y> column) {
    table.emplace_back(std::move(column));
  }
};

only pmr:

void new_data(std::pmr::vector<Y> column) {
   table.emplace_back(
     std::move(column), table.get_allocator());
}

, , .

0

Source: https://habr.com/ru/post/1681385/


All Articles