std::ostrstream::str(3)

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

πŸ’– NAME πŸ’–

okay bestie, this is std::ostrstream::str from the C++ Standard Libary manual. The name is literally std::ostrstream::str, and the short description is also std::ostrstream::str β€” no cap, it's that kind of page. πŸ’…

✨ SYNOPSIS ✨

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

Returns the pointer to the beginning of the buffer, after freezing it. Effectively calls rdbuf()->str().

πŸ¦‹ PARAMETERS πŸ¦‹

none. Literally zero parameters β€” she takes no arguments. 🫢

🌸 RETURN VALUE 🌸

Pointer to the beginning of the buffer in the associated std::strstreambuf, or a null pointer if no buffer is available.

πŸŽ€ NOTES πŸŽ€

  • Before a call to str() that uses the result as a C-string, the stream buffer must be null-terminated.
  • Regular output such as with stream << 1.2 does not store a null terminator, it must be appended explicitly, typically with the manipulator std::ends.
  • After a call to str(), dynamic streams become frozen.
⚠️ bestie, the freeze/leak thing is not a drill: a call to freeze(false) is required before exiting the scope in which this ostrstream object was created, otherwise the destructor will leak memory. Also, additional output to a frozen stream may be truncated once it reaches the end of the allocated buffer, which may leave the buffer not null-terminated.

πŸ’… EXAMPLE πŸ’…

the original page has a little // Run this code marker before the block, so here's the code:

#include <iostream>
#include <strstream>

int main()
{
    std::ostrstream dyn; // dynamically-allocated output buffer
    dyn << "Test: " << 1.23; // not adding std::ends to demonstrate append behavior
    std::cout << "The output stream holds \"";
    std::cout.write(dyn.str(), dyn.pcount()) << "\"\n";
    // the stream is now frozen due to str()
    dyn << " More text" << std::ends;
    std::cout << "The output stream holds \"";
    std::cout.write(dyn.str(), dyn.pcount()) << "\"\n";
    dyn.freeze(false);
}

πŸ“ POSSIBLE OUTPUT πŸ“

The stream holds "Test: 1.23"
The stream holds "Test: 1.23 More "

🧚 SEE ALSO 🧚

  • str marks the buffer frozen and returns the beginning pointer of the input sequence β€” it's a public member function of std::strstreambuf.

πŸ’– cute little sign-off: source/version is http://cppreference.com, date is 2024.06.10, and the page is std::ostrstream::str(3). stay hydrated and remember to freeze(false) before you dip ✨

πŸ“¦ where this page comes from