std::cerr,std::wcerr(3)
π NAME π
okay so bestie, welcome to std::cerr,std::wcerr from the C++ Standard Libary manual π. The NAME is literally std::cerr,std::wcerr - std::cerr,std::wcerr β yes, it says the same thing twice, and honestly? iconic. This is the page for the global error-stream girlies, and we're getting into every single detail, no cap. β¨
β¨ Synopsis β¨
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. fr fr, multi-thread safe from the jump. π§
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). the way it just flushes instantly?? obsessed. π₯
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). that tie() energy is real. π
π Notes π
The 'c' in the name refers to "character" (stroustrup.com FAQ); cerr means "character error (stream)" and wcerr means "wide character error (stream)". π
π Example π
Output to stderr via std::cerr flushes out the pending output on std::cout, while output to stderr via std::clog does not. iykyk, that's the whole point. π¦
// Run this code
#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()
π§ Defect reports π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. history homework but make it cute. π
- DR:
LWG 455- Applied to:
C++98 - Behavior as published:
std::cerr.tie()andstd::wcerr.tie()returned null pointers - Correct behavior: they return
&std::coutand&std::wcoutrespectively
- Applied to:
π«Ά See also π«Ά
Initβ initializes standard stream objects (public member class ofstd::ios_base)clogβ writes to the standard C error streamstderrwclogβ (global object)coutβ writes to the standard C output streamstdoutwcoutβ (global object)stdinβ expression of typeFILE*associated with the input streamstdoutβ expression of typeFILE*associated with the output streamstderrβ expression of typeFILE*associated with the error output stream (macro constant)
k that's the whole page, bestie π β source: http://cppreference.com; version/date: 2024.06.10; page: std::cerr,std::wcerr(3). mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms