Disabled exceptions and noexcept ()

std::swap declared as follows:

 template <class T> void swap (T& a, T& b) noexcept (is_nothrow_move_constructible<T>::value && is_nothrow_move_assignable<T>::value); 

If you disable exceptions in my program (for example, using -fno-exceptions for g ++ ), std::swap use move operations for my custom types if they are enabled with the move option, whether they are unoccupied or not?

EDIT: next question:

Having understood that std :: swap will always use moves if I have their type, my real question is what happens to traits like is_nothrow_move_assignable<> ?

Will std::vector always use moves when redistributing if my types have noexcept(true) move operations?

+6
source share
2 answers

The swap noexcept specification tells only the user where it can use swap without encountering an exception. implementation is almost always equivalent

 auto tmp = std::move(a); a = std::move(b); b = std::move(tmp); 

Which moves objects around if, and only if the overload resolution is selected by the move assignment operator and / or constructor.

+4
source

Yes. noexcept simply indicates that std::swap will not throw if T does not move the constructor and move the destination. This does not affect the behavior of the swap body that the move constructor T will use and move the destination, regardless of whether they are selected or not, and regardless of whether you are compiled with the exceptions turned on.

+2
source

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


All Articles