std::move_if_noexcept (3) - Linux Manuals
std::move_if_noexcept: std::move_if_noexcept
NAME
std::move_if_noexcept - std::move_if_noexcept
Synopsis
Defined in header <utility>
template< class T >
typename std::conditional<
!std::is_nothrow_move_constructible<T>::value && std::is_copy_constructible<T>::value, (since C++11)
const T&, (until C++14)
T&&
>::type move_if_noexcept(T& x) noexcept;
template< class T >
constexpr typename std::conditional<
!std::is_nothrow_move_constructible<T>::value && std::is_copy_constructible<T>::value, (since C++14)
const T&,
T&&
>::type move_if_noexcept(T& x) noexcept;
move_if_noexcept obtains an rvalue reference to its argument if its move constructor does not throw exceptions or if there is no copy constructor (move-only type), otherwise obtains an lvalue reference to its argument. It is typically used to combine move semantics with strong exception guarantee.
Parameters
x - the object to be moved or copied
Return value
std::move(x) or x, depending on exception guarantees.
Notes
This is used, for example, by std::vector::resize, which may have to allocate new storage and then move or copy elements from old storage to new storage. If an exception occurs during this operation, std::vector::resize undoes everything it did to this point, which is only possible if std::move_if_noexcept was used to decide whether to use move construction or copy construction. (unless copy constructor is not available, in which case move constructor is used either way and the strong exception guarantee may be waived)
Example
// Run this code
Output:
Complexity
Constant
See also
forward forwards a function argument
(C++11)
move obtains an rvalue reference
(C++11)