CKTIS / C++
std::cerr: the standard error stream
std::cerr is the narrow output stream associated with stderr, initialized with unitbuf and tied to std::cout.
Version: C++23
Syntax
namespace std {
extern ostream cerr;
}std::cerr from <iostream> is a global std::ostream writing to the buffer associated with C stderr. Its unitbuf flag is set by default, and cerr.tie() == &std::cout flushes cout before an operation. With unitbuf, the sentry destructor normally flushes cerr afterward when the stream is good and no exception is being unwound.
It is useful for diagnostics kept separate from normal stdout data, but a user may redirect both descriptors to the same destination. A message assembled from several << operations is not a transaction and can interleave with other threads’ output.
Stream failures set state bits; by default they need not throw unless an exceptions() mask requests it. unitbuf improves diagnostic timeliness at the cost of more frequent flushes. Logs requiring structure, rotation, or message atomicity need a dedicated facility.
Examples
#include <iostream>
#include <sstream>
int main() {
std::ostringstream captured;
auto* original = std::cerr.rdbuf(captured.rdbuf());
std::cerr << "error " << 7;
std::cerr.rdbuf(original);
std::cout << captured.str() << '\n';
}
Result
error 7
std::cerr → error 7