std::ostrstream::freeze(3)

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

std::ostrstream::freeze(3) πŸ’…

Ok bestie, this is the C++ Standard Libary manual page for std::ostrstream::freeze. (Yes, β€œLibary” β€” keeping the header spelling exact, no cap.) This one is deprecated in C++98 and removed in C++26, so it’s a little vintage-azard, but we’re gonna understand it anyway.

🌸 NAME 🌸

std::ostrstream::freeze - std::ostrstream::freeze

That’s the name line, fr fr.

✨ Synopsis ✨

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

If the stream is using a dynamically-allocated array for output, this disables automatic allocation/deallocation of the buffer when flag == true, and enables it when flag == false. It effectively calls rdbuf()->freeze(flag). Slay, that’s the whole vibe.

🫢 Notes 🫢

After a call to str(), dynamic streams become frozen automatically. Important: 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.

🚨 Bestie, do not forget the freeze(false) before that ostrstream goes out of scope β€” the destructor leaking memory is not the cute kind of plot twist. And writing more to a frozen stream can get truncated at the end of the allocated buffer, so don’t act surprised.

πŸŽ€ Parameters πŸŽ€

  • flag β€” the desired status. That’s it, that’s the parameter.

⭐ Return value ⭐

(none)

No return, just vibes.

🌈 Example 🌈

// Run this code

#include <iostream>
#include <strstream>

int main()
{
    std::ostrstream 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::ostrstream 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)

Later, bestie! πŸ’œ This page is from http://cppreference.com, dated 2024.06.10, and the page tag is std::ostrstream::freeze(3). Stay frozen (or unfrozen, responsibly). πŸ¦‹

πŸ“¦ where this page comes from