std::basic_stringbuf::swap(3)

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

πŸ’… std::basic_stringbuf::swap(3) πŸ’…

hey bestie, welcome to the C++ Standard Libary page for std::basic_stringbuf::swap(3) β€” yes, that's the manual name right there, and we are gonna get through it together 🫢


🌸 NAME 🌸

std::basic_stringbuf::swap - std::basic_stringbuf::swap

okay so the NAME is literally just the same thing twice, and honestly? iconic. no notes πŸ’€


✨ Synopsis ✨

here's the signature tea β˜• β€” and yes, the version notes matter:

  • void swap( basic_stringbuf& rhs ); β€” (since C++11) (until C++20)
  • void swap( basic_stringbuf& rhs ) noexcept(/* see below */); β€” (since C++20)

Swaps the state and the contents of *this and rhs.

🚨 UB ALERT, bestie: the behavior is undefined if Allocator does not propagate on swap and (since C++11) the allocators of *this and other are unequal. that's a hard no-cap "do not do this" situation 😭

πŸŽ€ Parameters πŸŽ€

  • rhs β€” another basic_stringbuf

πŸ’– Return value πŸ’–

(none)

literally nothing comes back. it's giving "the swap was the reward" ✨


πŸ”₯ Exceptions πŸ”₯

  • (since C++11) (until C++20) β€” May throw implementation-defined exceptions.
  • (since C++20) β€” noexcept specification:
noexcept(std::allocator_traits<Allocator>::propagate_on_container_swap::value || std::allocator_traits<Allocator>::is_always_equal::value)

πŸ¦‹ Notes πŸ¦‹

This function is called automatically when swapping std::stringstream objects. It is rarely necessary to call it directly.

so like… it's doing its thing behind the scenes, main character energy, you usually don't need to bother it πŸ’…


🌈 Example 🌈

        #include <iomanip>
        #include <iostream>
        #include <sstream>
        #include <string>

        int main()
        {
            std::istringstream one("one");
            std::ostringstream two("two");

            std::cout << "Before swap: one = " << std::quoted(one.str())
                      << ", two = " << std::quoted(two.str()) << ".\n";

            one.rdbuf()->swap(*two.rdbuf());

            std::cout << "After  swap: one = " << std::quoted(one.str())
                      << ", two = " << std::quoted(two.str()) << ".\n";
        }

πŸ“ Output: πŸ“

        Before swap: one = "one", two = "two".
        After  swap: one = "two", two = "one".

πŸ‘‘ See also πŸ‘‘

  • constructor β€” constructs a basic_stringbuf object (public member function)
  • swap (C++11) β€” swaps two string streams (public member function of std::basic_stringstream<CharT,Traits,Allocator>)

⭐ Category ⭐

  • conditionally noexcept

okay that's the whole page, mwah πŸ’‹ β€” source/version: http://cppreference.com, date: 2024.06.10, page: std::basic_stringbuf::swap(3). bye bestie, go swap something cute 🫢

πŸ“¦ where this page comes from