Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/io/cerr"

From cppreference.com
< cpp‎ | io
(Corrected parentheses (& has lower priority than !=))
m (Shorten template names. Use {{lc}} where appropriate.)
Line 1: Line 1:
 
{{cpp/title|cerr|wcerr}}
 
{{cpp/title|cerr|wcerr}}
 
{{cpp/io/basic_ostream/navbar}}
 
{{cpp/io/basic_ostream/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | iostream }}
+
{{dcl header | iostream }}
{{ddcl list item | num=1 | 1=
+
{{dcl | num=1 | 1=
 
extern std::ostream cerr;
 
extern std::ostream cerr;
 
}}
 
}}
{{ddcl list item | num=2| 1=
+
{{dcl | num=2| 1=
 
extern std::wostream wcerr;
 
extern std::wostream wcerr;
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
The global objects {{c|std::cerr}} and {{c|std::wcerr}} control output to a stream buffer of implementation-defined type (derived from {{c|std::streambuf}} and {{c|std::wstreambuf}}, respectively), associated with the standard C error output stream {{c|stderr}}.
+
The global objects {{lc|std::cerr}} and {{lc|std::wcerr}} control output to a stream buffer of implementation-defined type (derived from {{lc|std::streambuf}} and {{lc|std::wstreambuf}}, respectively), associated with the standard C error output stream {{lc|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 {{c|std::cerr}} in user code.
+
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 {{lc|std::cerr}} in user code.
  
 
Unless {{c|sync_with_stdio(false)}} has been issued, it is safe to concurrently access these objects from multiple threads for both formatted and unformatted output.
 
Unless {{c|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, {{c|1=(std::cerr.flags() & unitbuf) != 0}} (same for {{tt|wcerr}}) meaning that any output sent to these stream objects is immediately flushed to the OS (via {{c|std::basic_ostream::sentry}}'s destructor).
+
Once initialized, {{c|1=(std::cerr.flags() & unitbuf) != 0}} (same for {{tt|wcerr}}) meaning that any output sent to these stream objects is immediately flushed to the OS (via {{lc|std::basic_ostream::sentry}}'s destructor).
  
In addition, {{c|std::cerr.tie()}} returns {{c|&std::cout}} (same for {{tt|wcerr}} and {{tt|wcout}}), meaning that any output operation on {{c|std::cerr}} first executes {{c|std::cout.flush()}} (via {{c|std::basic_ostream::sentry}}'s constructor) {{mark since c++11}}
+
In addition, {{c|std::cerr.tie()}} returns {{c|&std::cout}} (same for {{tt|wcerr}} and {{tt|wcout}}), meaning that any output operation on {{lc|std::cerr}} first executes {{c|std::cout.flush()}} (via {{lc|std::basic_ostream::sentry}}'s constructor) {{mark since c++11}}
  
 
===Example===
 
===Example===
Line 50: Line 50:
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/io/ios_base/dcl list Init}}
+
{{dsc inc | cpp/io/ios_base/dcl list Init}}
{{dcl list template | cpp/io/dcl list clog}}
+
{{dsc inc | cpp/io/dcl list clog}}
{{dcl list template | cpp/io/dcl list cout}}
+
{{dsc inc | cpp/io/dcl list cout}}
{{dcl list end}}
+
{{dsc end}}
  
 
[[de:cpp/io/cerr]]
 
[[de:cpp/io/cerr]]

Revision as of 18:55, 31 May 2013

 
 
 
 
Defined in header <iostream>
extern std::ostream cerr;
(1)
extern std::wostream wcerr;
(2)

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)

Example

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::this_thread::sleep_for(std::chrono::seconds(2));
    std::cout << "...thread calls flush()" << std::endl;
}
 
int main()
{
    std::thread t1(f);
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::clog << "This output from main is not tie()'d to cout\n";
    std::cerr << "This output is tie()'d to cout\n";
    t1.join();
}

Output:

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