std::cerr, std::wcerr

来自cppreference.com
< cpp‎ | io

定义于头文件 <iostream>
extern std::ostream cerr;
(1)
extern std::wostream wcerr;
(2)
全球的对象std::cerrstd::wcerr控制的实现定义类型(std::streambufstd::wstreambuf,分别来自),与标准的C错误输出流输出到一个流缓冲stderr
原文:
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.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
保证这些对象被称为以前建造的第一个构造函数中的静态对象,保证他们活​​得比最后的静态对象的析构函数,因此,它始终是在用户代码中写std::cerr.
原文:
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.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
除非sync_with_stdio(false)已经发出,它是安全的格式化和未格式化的输出都从多个线程同时访问这些对象.
原文:
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.
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
初始化完成后,std::cerr.flags() & unitbuf != 0(相同的wcerr),这意味着任何发送到输出流对象是立即刷新到操作系统的(通过std::basic_ostream::sentry的析构函数).
原文:
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).
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。
此外,std::cerr.tie()回报&std::cout(相同的wcerrwcout),这意味着任何输出操作std::cerr第一次执行std::cout.flush()(通过std::basic_ostream::sentry的构造函数)(C++11 起)
原文:
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) (C++11 起)
文本通过谷歌翻译机器翻译。
你可以帮忙校正和验证翻译。点击此处查看指示。

[编辑] 示例

通过CERR刷新cout中的待处理的输出到stderr输出,输出到stderr,而没有通过堵塞
原文:
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();
}

输出:

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

[编辑] 另请参阅

初始化标准流对象
(std::ios_base 的公开成员类) [编辑]
写入标准 C 错误流 stderr
(全局对象) [编辑]
写入标准 C 输出流 stdout
(全局对象) [编辑]