Skip to content

Search topics & cppreference

Search all study topics, concept maps, and 6,587 C++ and C reference pages.

std::move_if_noexcept

Master pointer-stealing ownership transfer, noexcept reallocation cliffs in std::vector, and T&& template deduction.

Utility
  • C++11
  • C++14
1 min readView on cppreference
NotebookLM
[dcl.ref][temp.deduct.call][class.copy.ctor][utility.syn]

T&& is a forwarding reference ONLY when T is a deduced template parameter of that exact function—not std::vector<T>&&.

CppCon angle
Show the exact 24-byte stack footprint of std::vector: copying duplicates the heap allocation, moving copies 3 pointers and nulls the source.
Common audience trap
Forgetting noexcept on a custom move constructor, forcing std::vector::push_back to fall back to O(N) deep copies for exception safety.
Stage takeaway
Universal reference collapsing has one simple rule: '&' is infectious (& + anything = &). Only && + && yields &&.

The noexcept move constructor cliff

[utility.move.if.noexcept] & [vector.capacity]

std::vector::push_back promises the strong exception guarantee: if reallocation fails halfway through migrating elements from the old buffer to the new buffer, the vector remains unchanged.

If moving element #50 out of 100 throws an exception, the old buffer already has 49 moved-from husks that cannot be rolled back. Therefore std::vector uses std::move_if_noexcept—it moves ONLY when the move constructor is noexcept (or when no copy constructor exists).

noexcept unlocks fast vector growth
struct Buffer {
    int* data = nullptr;
    Buffer(Buffer&& other) noexcept   // <-- omit noexcept and vector COPIES!
        : data(std::exchange(other.data, nullptr)) {}
    Buffer(const Buffer& other);
};

Key rule: Always mark user-defined move constructors and move assignment operators noexcept whenever they cannot throw.

Forwarding reference vs. rvalue reference & collapsing

[temp.deduct.call] & [dcl.ref]

The syntax T&& means 'rvalue reference' everywhere EXCEPT when T is a deduced type parameter of that function template (or auto&&). Any cv-qualifier (const T&&) or wrapper (std::vector<T>&&) disables the forwarding-reference rule and becomes a plain rvalue reference.

When T&& IS a forwarding reference and receives an lvalue of type X, template deduction makes an intentional exception: T deduces to X&. Then reference collapsing fires: X& + && collapses to X&. Passing an rvalue deduces T = X, leaving X&&.

Reference collapsing matrix
template <class T>
void relay(T&& arg) {
    sink(std::forward<T>(arg));
}

Widget w;
relay(w);            // T = Widget&,  arg = Widget&  (lvalue)
relay(Widget{});     // T = Widget,   arg = Widget&& (xvalue after forward)
relay(std::move(w)); // T = Widget,   arg = Widget&&

Key rule: Any single & in a reference collapse (& &, & &&, && &) yields &; only && + && stays &&.

Across revisions (C++11 → C++20 → C++23)

C++11

Added rvalue references, reference collapsing, std::move, std::forward, and noexcept move detection.

C++23

Added Deducing This (explicit object parameters: void f(this Self&& self)) eliminating quadrupled &/const&/&&/const&& overloads, plus std::forward_like.

Full 1:1 cppreference specification

Original specification mirrored from cppreference.com with restyled declaration panels and code windows.

Defined in header <utility>
template< class T >
/* see below */ move_if_noexcept( T& x ) noexcept;
(since C++11)
(constexpr since C++14)

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 obtains an lvalue reference to its argument. It is typically used to combine move semantics with strong exception guarantee.

The return type of std::move_if_noexcept is:

Parameters

x - the object to be moved or copied

Return value

std::move(x) or x, depending on exception guarantees.

Complexity

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).

Example

example.cpp
#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

(C++11)
forwards a function argument and use the type template argument to preserve its value category
(function template)
(C++11)
converts the argument to an xvalue
(function template)

Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.