std::swap(std::optional)(3)

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

πŸ’– NAME πŸ’–

okay so this page is std::swap(std::optional) β€” like, literally std::swap(std::optional), bestie β€” and it comes from the C++ Standard Libary manual. yes, that's the manual name up top, folded in all cute so you don't have to read a crusty header line. 🫢


✨ Synopsis ✨

it's defined in the header <optional>, and here's the signature, line breaks and all, no cap:

Defined in header <optional>
template< class T >

void swap( std::optional<T>& lhs,                            (since
C++17)
                                                                       (constexpr
since C++20)
                     std::optional<T>& rhs ) noexcept(/* see below
          */);

This overloads the std::swap algorithm for std::optional. it exchanges the state of lhs with that of rhs, and effectively calls lhs.swap(rhs). iconic. πŸ’…

This overload participates in overload resolution only if std::is_move_constructible_v<T> and std::is_swappable_v<T> are both true. both of them. no exceptions to that little rule, bestie. ✨


🫢 Parameters 🫢

  • lhs, rhs β€” optional objects whose states to swap. yes, both of them, swapping their little states. πŸ¦‹

🌸 Return value 🌸

(none) β€” literally nothing comes back. it's giving "the swap was the point". 😭


πŸ”₯ Exceptions πŸ”₯

noexcept specification:

noexcept(noexcept(lhs.swap(rhs)))

that's the whole exception vibe, periodt. πŸŽ€


πŸŽ€ Example πŸŽ€

// Run this code

#include <iostream>
#include <optional>
#include <string>

int main()
{
    std::optional<std::string> a{"β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ"}, b{"β–’β–’β–’β–’β–’β–’"};

    auto print = [&](auto const& s)
    {
        std::cout << s << "\t"
                     "a = " << a.value_or("(null)") << "  "
                     "b = " << b.value_or("(null)") << '\n';
    };

    print("Initially:");
    std::swap(a, b);
    print("swap(a, b):");
    a.reset();
    print("\n""a.reset():");
    std::swap(a, b);
    print("swap(a, b):");
}

🌈 Output 🌈

Initially:   a = β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ  b = β–’β–’β–’β–’β–’β–’
swap(a, b):  a = β–’β–’β–’β–’β–’β–’  b = β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ

a.reset():   a = (null)  b = β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ
swap(a, b):  a = β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ  b = (null)

🧚 Defect reports 🧚

The following behavior-changing defect reports were applied retroactively to previously published C++ standards. history said "let's fix that" and honestly? slay. πŸ”₯

  • P2231R1 β€” applied to C++20.
    • Behavior as published: swap was not constexpr while the required operations can be constexpr in C++20.
    • Correct behavior: made constexpr.

πŸ‘‘ See also πŸ‘‘

  • swap β€” exchanges the contents (public member function). it's a public member function, iykyk. πŸ’œ

πŸ’… Category πŸ’…

  • * conditionally noexcept

source/version and date, cute sign-off style: this page is from http://cppreference.com, dated 2024.06.10, and it's page std::swap(std::optional)(3). bye bestie, go swap something responsibly πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from