std::strstream::freeze(3)

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

std::strstream::freeze(3)

okay so this is std::strstream::freeze(3), straight from the C++ Standard Libary manual β€” yes, that's how the manual name is spelled in the header, and we are not doing a spelling intervention today πŸ˜­πŸ’…


✨ NAME ✨

std::strstream::freeze β€” literally std::strstream::freeze, bestie. That's the whole name. No plot twist πŸ’€

πŸ“œ Synopsis πŸ“œ

void freeze( bool flag = true );  (deprecated in C++98)
                                  (removed in C++26)

so this function is deprecated in C++98 and removed in C++26. Vintage behavior, bestie ✨

🧊 Description 🧊

If the stream is using a dynamically-allocated array for output, this disables (flag == true) or enables (flag == false) automatic allocation/deallocation of the buffer.

It effectively calls rdbuf()->freeze(flag). Yep, it delegates. Slay.

🫢 Notes 🫢

After a call to str(), dynamic streams become frozen automatically. So the stream is now in its icy era 🧊✨

A call to freeze(false) is required before exiting the scope in which this strstream object was created, otherwise the destructor will leak memory. Like, not optional. Do not forget it, bestie 😭

Also, additional output to a frozen stream may be truncated once it reaches the end of the allocated buffer. So if your text gets cut off, that's why β€” vibe check failed πŸ’€

πŸŽ€ Parameters πŸŽ€

  • flag β€” desired status.

⭐ Return value ⭐

(none).

πŸ’– Example πŸ’–

// Run this code

#include <iostream>
#include <strstream>

int main()
{
    std::strstream dyn; // dynamically-allocated output buffer
    dyn << "Test: " << 1.23; // note: no std::ends to demonstrate appending
    std::cout << "The output stream contains \"";
    std::cout.write(dyn.str(), dyn.pcount()) << "\"\n";
    // the stream is now frozen due to str()
    dyn << " More text"; // output to a frozen stream may be truncated
    std::cout << "The output stream contains \"";
    std::cout.write(dyn.str(), dyn.pcount()) << "\"\n";
    dyn.freeze(false); // freeze(false) must be called or the destructor will leak

    std::strstream dyn2; // dynamically-allocated output buffer
    dyn2 << "Test: " << 1.23; // note: no std::ends
    std::cout << "The output stream contains \"";
    std::cout.write(dyn2.str(), dyn2.pcount()) << "\"\n";
    dyn2.freeze(false);   // unfreeze the stream after str()
    dyn2 << " More text" << std::ends; // output will not be truncated (buffer grows)
    std::cout << "The output stream contains \"" << dyn2.str() << "\"\n";
    dyn2.freeze(false); // freeze(false) must be called or the destructor will leak
}

🌈 Possible output:

The output stream contains "Test: 1.23"
The output stream contains "Test: 1.23 More "
The output stream contains "Test: 1.23"
The output stream contains "Test: 1.23 More text"

πŸ‘€ See also πŸ‘€

  • freeze sets/clears the frozen state of the buffer β€” public member function of std::strstreambuf.

that's the page, bestie 🫢 β€” source http://cppreference.com, dated 2024.06.10, and it's the std::strstream::freeze(3) page. periodt πŸ’…βœ¨

πŸ“¦ where this page comes from