std::move_if_noexcept(3)
Okay bestie, welcome to the std::move_if_noexcept(3) page from the C++ Standard Libary manual β yep, that's the manual name on the tin, and we're keeping it exactly as it is. π
π NAME π
std::move_if_noexcept β std::move_if_noexcept. That's the name, no cap. π
β¨ Synopsis β¨
Defined in header <utility> π¦
First overload, with the (since C++11) and (until C++14) annotations:
template< class T >
typename std::conditional<
!std::is_nothrow_move_constructible<T>::value &&
std::is_copy_constructible<T>::value,
const T&,
T&&
>::type move_if_noexcept( T& x ) noexcept;
(since C++11) (until C++14)
Second overload, now with constexpr and the (since C++14) annotation:
template< class T >
constexpr typename std::conditional<
!std::is_nothrow_move_constructible<T>::value &&
std::is_copy_constructible<T>::value,
const T&,
T&&
>::type move_if_noexcept( T& x ) noexcept;
(since C++14)
So here's the vibe: std::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 it obtains an lvalue reference to its argument. It's typically used to combine move semantics with the strong exception guarantee. Iconic behavior, honestly. β¨
π«Ά Parameters π«Ά
xβ the object to be moved or copied. That's the only parameter, bestie. π
π Return value π
It gives you std::move(x) or x, depending on exception guarantees. That's the whole return-value situation. π«Ά
β‘ Complexity β‘
Constant. Literally constant. β‘
π 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). That's the whole note, and it's lowkey important. π
π Example π
// Run this code
#include <iostream>
#include <utility>
struct Bad
{
Bad() {}
Bad(Bad&&) // may throw
{
std::cout << "Throwing move constructor called\n";
}
Bad(const Bad&) // may throw as well
{
std::cout << "Throwing copy constructor called\n";
}
};
struct Good
{
Good() {}
Good(Good&&) noexcept // will NOT throw
{
std::cout << "Non-throwing move constructor called\n";
}
Good(const Good&) noexcept // will NOT throw
{
std::cout << "Non-throwing copy constructor called\n";
}
};
int main()
{
Good g;
Bad b;
[[maybe_unused]] Good g2 = std::move_if_noexcept(g);
[[maybe_unused]] Bad b2 = std::move_if_noexcept(b);
}
Output:
Non-throwing move constructor called
Throwing copy constructor called
π See also π
forwardβ forwards a function argument(C++11)(function template)moveβ obtains an rvalue reference(C++11)(function template)
π Cute little sign-off time: source/version and date arehttp://cppreference.com,2024.06.10, andstd::move_if_noexcept(3). That's the footer folded into a vibe, bestie β stay noexcept. π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms