I/O manipulators | ||||
Print functions(C++23) | ||||
C-style I/O | ||||
Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
Streams | ||||
Abstractions | ||||
File I/O | ||||
String I/O | ||||
Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
Synchronized Output | ||||
(C++20) | ||||
Types | ||||
Error category interface | ||||
(C++11) | ||||
(C++11) |
Global objects | ||||
Member functions | ||||
(C++11) | ||||
Formatted output | ||||
Unformatted output | ||||
Positioning | ||||
Miscellaneous | ||||
basic_ostream::flush | ||||
(C++11) | ||||
Member classes | ||||
Non-member functions | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
basic_ostream& flush(); | ||
Writes uncommitted changes to the underlying output sequence. Behaves as anUnformattedOutputFunction.
Ifrdbuf() is a null pointer, the sentry object is not constructed.
Otherwise, after constructing and checking the sentry object, callsrdbuf()->pubsync(). If the call returns-1, callssetstate(badbit).
Contents |
*this
May throwstd::ios_base::failure if(exceptions()& badbit)!=0.
#include <chrono>#include <iostream>#include <thread> usingnamespace 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-timeif(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:
Output from mainOutput from thread... 1 2 3 4 5 6 7 8 9
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 581 | C++98 | flush() did not behave as anUnformattedOutputFunctionbecause of the resolution ofLWG issue 60 | behaves as an UnformattedOutputFunction |
invokessync() (public member function of std::basic_streambuf<CharT,Traits> )[edit] | |
[virtual] | synchronizes the buffers with the associated character sequence (virtual protected member function of std::basic_streambuf<CharT,Traits> )[edit] |
flushes the output stream (function template)[edit] | |
outputs'\n' and flushes the output stream (function template)[edit] | |
synchronizes with the underlying storage device (public member function of std::basic_istream<CharT,Traits> )[edit] |