std::swap(s...sic_string)(3)

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

πŸ’… std::swap(std::basic_string)

okay so this page is from the C++ Standard Libary (their spelling, not mine, and honestly the confidence is iconic πŸ’–). The title is std::swap(std::basic_string), and the NAME section is literally just that twice. Repetitive queen behavior, we love it.

πŸ’– NAME

std::swap(std::basic_string) - std::swap(std::basic_string)

✨ Synopsis

Defined in header <string>.

Until C++17, it's giving:

template< class CharT, class Traits, class Alloc >
void swap( std::basic_string<CharT, Traits, Alloc>& lhs,
           std::basic_string<CharT, Traits, Alloc>& rhs );

Since C++17, and constexpr since C++20, it's giving:

template< class CharT, class Traits, class Alloc >
void swap( std::basic_string<CharT, Traits, Alloc>& lhs,
           std::basic_string<CharT, Traits, Alloc>& rhs )
    noexcept(/* see below */);

This specializes the std::swap algorithm for std::basic_string. It swaps the contents of lhs and rhs. Simple. Clean. Slay.

Equivalent to lhs.swap(rhs).

🫢 Parameters

  • lhs, rhs β€” the strings whose contents get swapped. That's the whole assignment, bestie.

🌸 Return value

(none) β€” it gives you nothing back. No return value, no crumbs, just vibes.

πŸ¦‹ Complexity

Constant. Constant time, periodt.

πŸ”₯ Exceptions

Since C++17, the noexcept specification is:

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

πŸŽ€ Example

Run this code, bestie:

// Run this code

#include <iostream>
#include <string>

int main()
{
    std::string a = "AAA";
    std::string b = "BBBB";

    std::cout << "Before swap:\n"
                 "a = " << a << "\n"
                 "b = " << b << "\n\n";

    std::swap(a, b);

    std::cout << "After swap:\n"
                 "a = " << a << "\n"
                 "b = " << b << '\n';
}

πŸ’œ Output

It prints exactly this:

Before swap:
a = AAA
b = BBBB

After swap:
a = BBBB
b = AAA

πŸ‘‘ Defect reports

These behavior-changing defect reports got applied retroactively to previously published C++ standards, no cap:

  • LWG 2064 β€” applied to C++11. Behavior as published: non-member swap was noexcept and inconsistent with member swap. Correct behavior: noexcept removed.

🌈 See also

  • swap β€” swaps the contents (public member function).

⭐ Category

  • * conditionally noexcept

okay that's the whole page bestie ✨ β€” source http://cppreference.com, version/date 2024.06.10, page std::swap(s...sic_string)(3). mwah πŸ’–

πŸ“¦ where this page comes from