Title
Should swapping a built-in array or `std::array` with itself result in UB?
Status
new
Section
[concept.swappable][utility.swap][array.members]
Submitter
Jiang An

Created on 2024-10-13.00:00:00 last changed 1 month ago

Messages

Date: 2024-10-13.00:00:00

Currently, the `std::swap` overload for built-in arrays, the `swap` member function of `std::array`, the `std::swap` overload for `std::array`, and the expression-equivalent-to operation of `std::ranges::swap` for built-in arrays are specified to be equivalent to performing `std::(ranges::)swap_ranges`. As `swap_ranges` functions require that the swapped ranges shall not overlap ([alg.swap]/2), swapping a built-in array or a nonzero-length std::array with itself results in undefined behavior.

It seems that no other swap operation in the standard library has precondition that both arguments don't refer to the same object. Should we make self-swapping of arrays well-defined? If so, perhaps we should change the semantics to plain loops that execute `swap` or `ranges::swap` on elements.

History
Date User Action Args
2024-10-13 00:00:00admincreate