std::basic_ostream::flush(3)
π std::basic_ostream::flush(3) π
Hey bestie!! Welcome to the C++ Standard Libary page for std::basic_ostream::flush β yes, that spelling is exactly what the header says, and we're keeping it, no cap π. This one is short and sweet: it's the function that writes uncommitted changes to the underlying output sequence. Let's get into it β¨
β¨ NAME β¨
std::basic_ostream::flush β std::basic_ostream::flush. That's the whole NAME section. It said the thing twice and honestly? iconic. Periodt π
πΈ Synopsis πΈ
basic_ostream& flush();
Okay so what does it do? It writes uncommitted changes to the underlying output sequence. It behaves as an UnformattedOutputFunction β that's the vibe check π¦.
Now the details, bestie: If rdbuf() is a null pointer, the sentry object is not constructed. Otherwise, after constructing and checking the sentry object, it calls rdbuf()->pubsync(). If that call returns -1, it calls setstate(badbit). That's the whole flow π
.
π Parameters π
(none) β literally no parameters. Not a single one. We love a no-argument queen π«Ά.
π¦ Return value π¦
*this β it returns itself. Main character energy, fr fr π.
π₯ Exceptions π₯
May throw std::ios_base::failure if (exceptions() & badbit) != 0. That's the condition, copy-pasted with love π.
π Example π
The page says // Run this code, so here is the code, exactly:
#include <chrono>
#include <iostream>
#include <thread>
using namespace std::chrono_literals;
void f()
{
std::cout << "Output from thread... ";
for (int i{1}; i != 10; ++i)
{
std::this_thread::sleep_for(250ms);
std::cout << i << ' ';
// output three numbers at once;
// the effect is observable only in real-time
if (0 == (i % 3))
std::cout.flush();
}
std::cout << std::endl; // flushes as well
}
int main()
{
std::thread tr{f};
std::this_thread::sleep_for(150ms);
std::clog << "Output from main\n";
tr.join();
}
β Output β
Here's what it prints:
Output from main
Output from thread... 1 2 3 4 5 6 7 8 9
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- DR:
LWG 581 - Applied to:
C++98 - Behavior as published:
flush()did not behave as anUnformattedOutputFunctionbecause of the resolution of LWG issue 60 - Correct behavior: behaves as an
UnformattedOutputFunction
π See also π
pubsyncβ invokessync()(public member function ofstd::basic_streambuf<CharT,Traits>)syncβ synchronizes the buffers with the associated character sequence[virtual](virtual protected member function ofstd::basic_streambuf<CharT,Traits>)flushβ flushes the output stream (function template)endlβ outputs'\n'and flushes the output stream (function template)syncβ synchronizes with the underlying storage device (public member function ofstd::basic_istream<CharT,Traits>)
That's the whole page, bestie. It came from http://cppreference.com, dated 2024.06.10 β mwah πβ¨
π¦ 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