Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/io/cerr"

From cppreference.com
< cpp‎ | io
m (Text replace - "{{example cpp" to "{{example")
m (http -> https, capitalized 1st letter)
 
(16 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 
{{cpp/title|cerr|wcerr}}
 
{{cpp/title|cerr|wcerr}}
{{cpp/io/basic_ostream/sidebar}}
+
{{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 {{cpp|std::cerr}} and {{cpp|std::wcerr}} control output to a stream buffer of implementation-defined type (derived from {{cpp|std::streambuf}} and {{cpp|std::wstreambuf}}, respectively), associated with the standard C error output stream {{cpp|stderr}}.
+
The global objects {{tt|std::cerr}} and {{tt|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 {{cpp|std::cerr}} in user code.
+
These objects are guaranteed to be initialized during or before the first time an object of type {{lc|std::ios_base::Init}} is constructed and are available for use in the constructors and destructors of static objects with [[cpp/language/initialization#Non-local_variables|ordered initialization]] (as long as {{header|iostream}} is included before the object is defined).
  
Unless {{cpp|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|std::ios_base::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, {{cpp|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.
+
Once initialized, {{c|1=(std::cerr.flags() & unitbuf) != 0}} (same for {{tt|std::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, {{cpp|std::cerr.tie()}} returns {{cpp|&std::cout}} (same for {{tt|wcerr}} and {{tt|wcout}}), meaning that any output operation on {{cpp|std::cerr}} first executes {{cpp|std::cout.flush()}} (via {{cpp|std::basic_ostream::sentry}}'s constructor) {{mark since c++11}}
+
In addition, {{c|std::cerr.tie()}} returns {{c|&std::cout}} (same for {{tt|std::wcerr}} and {{lc|std::wcout}}), meaning that any output operation on {{tt|std::cerr}} first executes {{c|std::cout.flush()}} (via {{lc|std::basic_ostream::sentry}}'s constructor).
 +
 
 +
===Notes===
 +
The 'c' in the name refers to "character" ([https://www.stroustrup.com/bs_faq2.html#cout stroustrup.com FAQ]); {{tt|cerr}} means "character error (stream)" and {{tt|wcerr}} means "wide character error (stream)".
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| output to stderr via cerr flushes out the pending output on cout, while output to stderr via clog does not
+
|Output to {{lc|stderr}} via {{tt|std::cerr}} flushes out the pending output on {{lc|std::cout}}, while output to {{lc|stderr}} via {{lc|std::clog}} does not.
| code=
+
|code=
#include <thread>
+
#include <iostream>
+
 
#include <chrono>
 
#include <chrono>
 +
#include <iostream>
 +
#include <thread>
 +
using namespace std::chrono_literals;
 +
 
void f()
 
void f()
 
{
 
{
 
     std::cout << "Output from thread...";
 
     std::cout << "Output from thread...";
     std::this_thread::sleep_for(std::chrono::seconds(2));
+
     std::this_thread::sleep_for(2s);
 
     std::cout << "...thread calls flush()" << std::endl;
 
     std::cout << "...thread calls flush()" << std::endl;
 
}
 
}
Line 37: Line 42:
 
int main()
 
int main()
 
{
 
{
     std::thread t1(f);
+
     std::jthread t1{f};
     std::this_thread::sleep_for(std::chrono::seconds(1));
+
     std::this_thread::sleep_for(1000ms);
 
     std::clog << "This output from main is not tie()'d to cout\n";
 
     std::clog << "This output from main is not tie()'d to cout\n";
 
     std::cerr << "This output is tie()'d to cout\n";
 
     std::cerr << "This output is tie()'d to cout\n";
    t1.join();
 
 
}
 
}
| output=
+
|p=true
 +
|output=
 
This output from main is not tie()'d to cout
 
This output from main is not tie()'d to cout
 
Output from thread...This output is tie()'d to cout
 
Output from thread...This output is tie()'d to cout
 
...thread calls flush()
 
...thread calls flush()
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=455|std=C++98|before={{c|std::cerr.tie()}} and<br>{{c|std::wcerr.tie()}} returned null pointers|after=they return {{c|&std::cout}} and<br>{{c|&std::wcout}} respectively}}
 +
{{dr list end}}
  
 
===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/dsc Init}}
{{dcl list template | cpp/io/dcl list clog}}
+
{{dsc inc|cpp/io/dsc clog}}
{{dcl list template | cpp/io/dcl list cout}}
+
{{dsc inc|cpp/io/dsc cout}}
{{dcl list end}}
+
{{dsc inc|cpp/io/c/dsc std streams}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 11:29, 11 September 2023

 
 
 
 
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 initialized during or before the first time an object of type std::ios_base::Init is constructed and are available for use in the constructors and destructors of static objects with ordered initialization (as long as <iostream> is included before the object is defined).

Unless std::ios_base::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 std::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 std::wcerr and std::wcout), meaning that any output operation on std::cerr first executes std::cout.flush() (via std::basic_ostream::sentry's constructor).

Contents

[edit] Notes

The 'c' in the name refers to "character" (stroustrup.com FAQ); cerr means "character error (stream)" and wcerr means "wide character error (stream)".

[edit] Example

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

#include <chrono>
#include <iostream>
#include <thread>
using namespace std::chrono_literals;
 
void f()
{
    std::cout << "Output from thread...";
    std::this_thread::sleep_for(2s);
    std::cout << "...thread calls flush()" << std::endl;
}
 
int main()
{
    std::jthread t1{f};
    std::this_thread::sleep_for(1000ms);
    std::clog << "This output from main is not tie()'d to cout\n";
    std::cerr << "This output is tie()'d to cout\n";
}

Possible output:

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

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 455 C++98 std::cerr.tie() and
std::wcerr.tie() returned null pointers
they return &std::cout and
&std::wcout respectively

[edit] See also

initializes standard stream objects
(public member class of std::ios_base) [edit]
writes to the standard C error stream stderr
(global object)[edit]
writes to the standard C output stream stdout
(global object)[edit]
expression of type FILE* associated with the input stream
expression of type FILE* associated with the output stream
expression of type FILE* associated with the error output stream
(macro constant) [edit]