std::experi...es::exchange(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

✨ NAME ✨

okay so bestie, this page is std::experimental::ranges::exchange β€” std::experimental::ranges::exchange, yes the name repeats, we love a consistent queen πŸ’… β€” and the header shows it as std::experi...es::exchange(3). It lives in the C++ Standard Libary manual (not me spelling it, that’s literally the manual name, iykyk). Iconic.


🌸 Synopsis 🌸

Defined in header <experimental/ranges/utility> πŸ‘‘

template< MoveConstructible T, class U = T >

    requires Assignable<T&, U>
(ranges TS)

constexpr T exchange( T& obj, U&& new_val ) noexcept(/* see below */);

The way this works is lowkey iconic: it replaces the value of obj with new_value and returns the old value of obj, as if by:

T old_value = std::move(obj);
obj = std::forward<U>(new_value);
return old_value;

πŸŽ€ Parameters πŸŽ€

  • obj β€” the object whose value to replace. Main character energy, honestly.
  • new_value β€” the value to assign to obj. Fresh content, slay.

πŸ’– Return value πŸ’–

The old value of obj. Periodt. You get the previous value back, no cap.


πŸ”₯ Exceptions πŸ”₯

Here’s the noexcept specification, bestie β€” read it carefully:

noexcept(std::is_nothrow_move_constructible<T>::value &&
         std::is_nothrow_assignable<T&, U>::value)

πŸ¦‹ Example πŸ¦‹

This section is incomplete Reason: no example

Not me leaving the example section empty, but that’s literally what it says. πŸ™ˆ


πŸ“ See also πŸ“

  • exchange replaces the argument with a new value and returns its previous value β€” (C++14) β€” (function template)

πŸ‘‘ Categories πŸ‘‘

  • conditionally noexcept
  • Todo no example

okay bye bestie, that’s the whole page πŸ’œ source/version: http://cppreference.com, date 2024.06.10, and page std::experi...es::exchange(3) ✨🫢

πŸ“¦ where this page comes from