Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is no std::uninitialized_move_if_noexcept?

C++17 adds std::uninitialized_move, but there is no std::uninitialized_move_if_noexcept that would use std::move_if_noexcept internally. In my opinion, it would be useful, since now, if we want to reallocate, we still need to write something as

if constexpr (!std::is_nothrow_move_constructible_v<value_type>
              && std::is_copy_constructible_v<value_type>)
  std::uninitialized_copy(...);
else 
  std::uninitialized_move(...); 

Are there any particular reasons why std::uninitialized_move_if_noexcept was not introduced in C++17?

like image 308
Daniel Langr Avatar asked Oct 23 '18 07:10

Daniel Langr


1 Answers

A paper on "Extending memory management tools" at open-std.org has a section on uninitialized_move which deals with this matter.

Some concern is raised about exception handling with respect to uninitialized_move. If a move-constructor throws, the source object may have been irreparably damaged. As there is no solution to this problem, we implement the natural and expected semantics of destroying all fully constructed objects in the destination buffer and propagating the exception. This matches the behavior of uninitialized_copy as closely as possible. An additional algorithm, uninitialized_move_if_noexcept, could be considered as a resolution to this concern. Such an algorithm was found already implemented in libstdc++ using a move_if_noexcept iterator. Given that there is currently no range-based move_if_noexcept algorithm, such a solution is not considered here. It seems clear that such a feature is readily possible, however.

like image 62
P.W Avatar answered Oct 16 '22 20:10

P.W