The small, foundational free functions that show up in almost every modern C++ file:
move,forward,swap,exchange, and friends. Header:<utility>
std::move moves nothing. It's a cast to an rvalue reference that says "I'm
done with this; you may steal its guts." Whether anything is stolen is up to the
move constructor/assignment that receives it.
std::string a = "hello";
std::string b = std::move(a); // b steals a's buffer; a is now valid-but-unspecified
// reading a's value is allowed but meaningless; assigning a new value is fineIn a function template taking a forwarding reference T&&, std::forward<T>
preserves the original value category (lvalue stays lvalue, rvalue stays rvalue)
when passing it on. Use it only with deduced T&&.
template<class T>
void relay(T&& x) {
sink(std::forward<T>(x)); // lvalue→lvalue, rvalue→rvalue
}Rule of thumb: move an rvalue you own; forward a forwarding reference.
int x = 1, y = 2;
std::swap(x, y); // x==2, y==1Provide an ADL-findable swap for your type (or rely on member swap); the
standard library uses the customization point. Most std types specialize it to
be O(1).
int x = 7;
int old = std::exchange(x, 42); // x becomes 42, `old` holds the previous valueHandy in move constructors (ptr_ = std::exchange(other.ptr_, nullptr);) and
loop accumulators.
Comparing signed and unsigned directly is a classic bug (-1 < 0u is false).
The cmp_* functions compare by mathematical value:
#include <utility>
int x = -1; unsigned y = 0;
std::cmp_less(-1, 0u); // true (correct), unlike -1 < 0u
std::cmp_greater_equal(x, y);
std::in_range<std::size_t>(-1); // false — does -1 fit in size_t?Family: cmp_equal, cmp_not_equal, cmp_less, cmp_greater,
cmp_less_equal, cmp_greater_equal, in_range.
std::string obj = "text";
enum class Color : unsigned char { red, green };
Color some_enum = Color::green;
std::as_const(obj); // C++17: view as const (force const overloads)
std::to_underlying(some_enum); // C++23: enum → its underlying integer
auto [a, b] = std::pair{1, 2}; // std::pair / std::make_pair live here too
std::unreachable(); // C++23: mark code as never reached (UB if it is)std::moveon aconstobject moves nothing — the move ctor can't bind toconst&&, so it silently copies. Don'tmovefromconst.- Don't use a moved-from object's value. It's valid (destructible, assignable) but unspecified — assign before reading.
std::forwardwith the wrong type defeats forwarding. Alwaysstd::forward<T>(x)whereTis the deduced template parameter, never on a named rvalue you own (usemovethere).std::movein areturnof a local is usually wrong — it can disable copy elision (NRVO). Justreturn local;.