std::basic_ostream::flush(3)

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

πŸ’– 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 an UnformattedOutputFunction because of the resolution of LWG issue 60
  • Correct behavior: behaves as an UnformattedOutputFunction

πŸ’œ See also πŸ’œ

  • pubsync β€” invokes sync() (public member function of std::basic_streambuf<CharT,Traits>)
  • sync β€” synchronizes the buffers with the associated character sequence [virtual] (virtual protected member function of std::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 of std::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