std::unitbu...d::nounitbuf(3)
π 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? π
- Enables the
unitbufflag in the streamstras if by callingstr.setf(std::ios_base::unitbuf). - Disables the
unitbufflag in the streamstras if by callingstr.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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms