std::exchange(3)
π std::exchange π
okay bestie, welcome to the C++ Standard Libary, section 3 β this page is literally all about std::exchange. no cap, it's a tiny utility with main-character energy.
πΈ NAME πΈ
std::exchange - std::exchange
π Synopsis π
it's defined in header <utility>, periodt.
template< class T, class U = T >
T exchange( T& obj, U&& new_value );
(since C++14) (until C++20)
template< class T, class U = T >
constexpr T exchange( T& obj, U&& new_value );
(since C++20) (until C++23)
template< class T, class U = T >
constexpr T exchange( T& obj, U&& new_value ) noexcept(/* see below */);
(since C++23)
It replaces the value of obj with new_value and returns the old value of obj. literally a glow-up for obj, and you get the old vibe back. π
π§Έ Parameters π§Έ
objβ object whose value to replace.new_valueβ the value to assign toobj.
π¦ Type requirements π¦
Tmust meet the requirements ofMoveConstructible.- Also, it must be possible to move-assign objects of type
Uto objects of typeT.
π Return value π
The old value of obj. that's the whole gift, bestie π«Ά
π₯ Exceptions π₯
(none)until C++23.- noexcept specification since C++23:
noexcept(
std::is_nothrow_move_constructible_v<T> &&
std::is_nothrow_assignable_v<T&, U>
)
π οΈ Possible implementation π οΈ
template<class T, class U = T>
constexpr // Since C++20
T exchange(T& obj, U&& new_value)
noexcept( // Since C++23
std::is_nothrow_move_constructible<T>::value &&
std::is_nothrow_assignable<T&, U>::value
)
{
T old_value = std::move(obj);
obj = std::forward<U>(new_value);
return old_value;
}
π Notes π
The std::exchange can be used when implementing move assignment operators and move constructors:
struct S
{
int n;
S(S&& other) noexcept : n{std::exchange(other.n, 0)} {}
S& operator=(S&& other) noexcept
{
n = std::exchange(other.n, 0); // Move n, while leaving zero in other.n
// (note: in self-move-assignment, n is unchanged)
return *this;
}
};
π§ͺ Feature-test macro π§ͺ
- Feature-test macro:
__cpp_lib_exchange_function - Value:
201304L - Std:
(C++14) - Feature:
std::exchange
β¨ Example β¨
#include <iostream>
#include <iterator>
#include <utility>
#include <vector>
class stream
{
public:
using flags_type = int;
public:
flags_type flags() const { return flags_; }
// Replaces flags_ by newf, and returns the old value.
flags_type flags(flags_type newf) { return std::exchange(flags_, newf); }
private:
flags_type flags_ = 0;
};
void f() { std::cout << "f()"; }
int main()
{
stream s;
std::cout << s.flags() << '\n';
std::cout << s.flags(12) << '\n';
std::cout << s.flags() << "\n\n";
std::vector<int> v;
// Since the second template parameter has a default value, it is possible
// to use a braced-init-list as second argument. The expression below
// is equivalent to std::exchange(v, std::vector<int>{1, 2, 3, 4});
std::exchange(v, {1, 2, 3, 4});
std::copy(begin(v), end(v), std::ostream_iterator<int>(std::cout, ", "));
std::cout << "\n\n";
void (*fun)();
// The default value of template parameter also makes possible to use a
// normal function as second argument. The expression below is equivalent to
// std::exchange(fun, static_cast<void(*)()>(f))
std::exchange(fun, f);
fun();
std::cout << "\n\nFibonacci sequence: ";
for (int a{0}, b{1}; a < 100; a = std::exchange(b, a + b))
std::cout << a << ", ";
std::cout << "...\n";
}
π Output π
0
0
12
1, 2, 3, 4,
f()
Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
π See also π
swapβ swaps the values of two objects (function template).atomic_exchangeβ atomically replaces the value of the atomic object with non-atomic argument and returns the old value of the atomic (function template)(C++11).atomic_exchange_explicitβ atomically replaces the value of the atomic object with non-atomic argument and returns the old value of the atomic (function template)(C++11).
π·οΈ Category π·οΈ
- * conditionally noexcept
shoutout to cppreference.com for the source, and this version is dated 2024.06.10 β page std::exchange(3). mwah ππ«Ά
π¦ 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