std::unitbu...d::nounitbuf(3)

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

πŸ’– NAME πŸ’–

Okay bestie, this is the C++ Standard Libary man page for std::unitbu...d::nounitbuf(3). The NAME line is literally:

std::unitbuf,std::nounitbuf - std::unitbuf,std::nounitbuf

No, I’m not gonna make it cuter than that β€” that’s the actual name, periodt. πŸ’…

πŸ¦‹ Synopsis πŸ¦‹

Defined in header <ios> β€” here are the exact signatures:

std::ios_base& unitbuf( std::ios_base& str );   (1)
std::ios_base& nounitbuf( std::ios_base& str ); (2)

These bad boys either turn on or turn off automatic flushing of the output stream after any output operation. Has no effect on input. Like, input is not invited to this party, mmkay? 😌

  1. Enables the unitbuf flag in the stream str as if by calling str.setf(std::ios_base::unitbuf).
  2. Disables the unitbuf flag in the stream str as if by calling str.unsetf(std::ios_base::unitbuf).

It’s an I/O manipulator, so it may be called with an expression such as out << std::unitbuf for any out of type std::basic_ostream, or with an expression such as in >> std::unitbuf for any in of type std::basic_istream. Yep, both examples use std::unitbuf in the original β€” not me, I’m just the messenger. 🫢

🌸 Notes 🌸

The flushing is performed in the destructor of the std::basic_ostream::sentry object, which calls str.rdbuf()->pubsync() if str.flags() & std::ios_base::unitbuf is true. That’s the whole mechanism, no cap. πŸ”₯

The standard output objects std::cerr and std::wcerr have their unitbuf bit set by default. So they’re already doing the most. πŸ‘‘

πŸŽ€ Parameters πŸŽ€

  • str β€” reference to I/O stream

✨ Return value ✨

str (reference to the stream after manipulation).

πŸ“ Example πŸ“

Without std::unitbuf or another explicit flush, the output is the same, but does not appear in real time. That’s the vibe check: same bytes, but not the same live-update energy. 😭

The page says // Run this code, so here’s the code exactly:

#include <chrono>
#include <iostream>

template<typename Diff>
void log_progress(Diff d)
{
    std::cout <<
std::chrono::duration_cast<std::chrono::milliseconds>(d)
              << " ... ";
}

int main()
{
    volatile int sink = 0;
    std::cout << std::unitbuf; // enable automatic flushing

    const auto start = std::chrono::high_resolution_clock::now();
    for (int j = 0; j < 5; ++j)
    {
        for (int n = 0; n < 10000; ++n)
            for (int m = 0; m < 20000; ++m)
                sink += m * n; // do some work
        log_progress(std::chrono::high_resolution_clock::now() -
start);
    }
    std::cout << '\n';
}

πŸ’œ Output πŸ’œ

571ms ... 1146ms ... 1722ms ... 2294ms ... 2865ms ...

🌈 See also 🌈

  • flush β€” flushes the output stream (function template)
  • endl β€” outputs '\n' and flushes the output stream (function template)

Source/version: http://cppreference.com β€” date: 2024.06.10 β€” page: std::unitbu...d::nounitbuf(3). Love u, bestie β€” go flush responsibly! πŸ’‹βœ¨

πŸ“¦ where this page comes from