std::strstreambuf::str(3)

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

πŸ’… std::strstreambuf::str β€” C++ Standard Libary (3) ✨

okay bestie, welcome to the <pink>C++ Standard Libary</pink> page for std::strstreambuf::str(3) β€” this one is a whole little cursed-but-iconic legacy vibe, and we're gonna read it together. 🫢


🌸 NAME 🌸

std::strstreambuf::str β€” std::strstreambuf::str. yes, that's the name, she's serving "function that is literally named after herself" energy πŸ’…

✨ Synopsis ✨

the signature, preserved with love:

char* str();  (deprecated in C++98)
              (removed in C++26)

so here's what she does: calls freeze(), then returns a copy of the start pointer of the get area, std::streambuf::eback(). 🧚

and for all writeable std::strstreambuf objects constructed through the interface provided by std::strstream, the start of the get area is also the start of the put area. iykyk πŸ’«

πŸŽ€ Parameters πŸŽ€

  • (none) β€” literally not a single parameter, zero, nada, the void is empty and slaying 🌈

πŸ’– Return value πŸ’–

a copy of eback(), which may be a null pointer. so yes, bestie, check it before you wreck it β€” null is on the menu 😭

πŸ”₯ Notes πŸ”₯

this function is typically called through the std::strstream interface.

the call to freeze() guarantees that the returned pointer remains valid until the next explicit call to freeze(false): otherwise (on a dynamic buffer) any output operation could trigger buffer reallocation which would invalidate the pointer. that's the whole "don't let the buffer move while you're holding the pointer" situation, fr fr 🫢

🚨 warning bestie: it also causes a memory leak in the destructor of std::strstreambuf, unless freeze(false) is called before the buffer (or, more commonly, the std::strstream that manages it) is destroyed. not me warning you about a leak and then still thinking the example is cute πŸ’€

πŸ¦‹ Example πŸ¦‹

the page gives you a // Run this code moment, and the code is:

        #include <iostream>
        #include <strstream>

        int main()
        {
            std::strstream dyn; // dynamically-allocated read/write buffer
            dyn << "Test: " << 1.23 << std::ends;
            std::strstreambuf* buf = dyn.rdbuf();
            std::cout << "R/W buffer holds [" << buf->str() // or dyn.str()
                      << "]\n";
            dyn.freeze(false); // after calling .str() on a dynamic strstream

            char arr[10];
            std::ostrstream user(arr, 10); // fixed-size write-only buffer
            buf = user.rdbuf();
            user << 1.23 << std::ends;
            std::cout << "Write-only buffer holds [" << buf->str() // or
       user.str()
                      << "]\n";

            std::istrstream lit("1 2 3"); // fixed-size read-only buffer
            buf = lit.rdbuf();
            std::cout << "Read-only buffer holds [" << buf->str() // or
       lit.str()
                      << "]\n";
        }

⭐ Output ⭐

R/W buffer holds [Test: 1.23]
Write-only buffer holds [1.23]
Read-only buffer holds [1 2 31 2 3]

πŸ‘‘ See also πŸ‘‘

  • str accesses the output buffer β€” public member function of std::strstream
  • str accesses the output buffer β€” public member function of std::ostrstream
  • str accesses the output buffer β€” public member function of std::istrstream

mwah πŸ’‹ β€” source: http://cppreference.com, date: 2024.06.10, page: std::strstreambuf::str(3). byeee, go call freeze(false) and hydrate πŸ“πŸ«Άβœ¨

πŸ“¦ where this page comes from