std::cerr, std::wcerr

< cpp‎ | io
Revision as of 06:17, 6 January 2013 by (Talk)

Template:ddcl list begin <tr class="t-dsc-header">

Defined in header <iostream>

<td></td> <td></td> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
extern std::ostream cerr;

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
extern std::wostream wcerr;

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

The global objects std::cerr and std::wcerr control output to a stream buffer of implementation-defined type (derived from std::streambuf and std::wstreambuf, respectively), associated with the standard C error output stream stderr.

These objects are guaranteed to be constructed before the first constructor of a static object is called and they are guaranteed to outlive the last destructor of a static object, so that it is always possible to write to std::cerr in user code.

Unless sync_with_stdio(false) has been issued, it is safe to concurrently access these objects from multiple threads for both formatted and unformatted output.

Once initialized, (std::cerr.flags() & unitbuf) != 0 (same for wcerr) meaning that any output sent to these stream objects is immediately flushed to the OS (via std::basic_ostream::sentry's destructor).

In addition, std::cerr.tie() returns &std::cout (same for wcerr and wcout), meaning that any output operation on std::cerr first executes std::cout.flush() (via std::basic_ostream::sentry's constructor) (since C++11)


output to stderr via cerr flushes out the pending output on cout, while output to stderr via clog does not

#include <thread>
#include <iostream>
#include <chrono>
void f()
    std::cout << "Output from thread...";
    std::cout << "...thread calls flush()" << std::endl;
int main()
    std::thread t1(f);
    std::clog << "This output from main is not tie()'d to cout\n";
    std::cerr << "This output is tie()'d to cout\n";


This output from main is not tie()'d to cout
Output from thread...This output is tie()'d to cout
...thread calls flush()

See also

Template:cpp/io/ios base/dcl list InitTemplate:cpp/io/dcl list clogTemplate:cpp/io/dcl list cout