std::cerr
STD::cerr,std::wcerr
Defined in header | | |
---|---|---|
extern std::ostream cerr; | (1) | |
extern std::wostream wcerr; | (2) | |
全局对象std::cerr
和std::wcerr
控件输出到实现定义类型%28的流缓冲区。std::streambuf
和std::wstreambuf
,分别为%29,与标准C错误输出流相关联stderr
...
这些对象保证在第一次类型对象时或之前初始化。std::ios_base::Init构造,并可在静态对象的构造函数和析构函数中使用有序初始化%28<iostream>对象定义%29之前包含。
除非sync_with_stdio(false)
发出后,可以安全地从多个线程中同时访问这些对象,用于格式化输出和未格式化输出。
一旦初始化,(std::cerr.flags() & unitbuf) != 0
28%wcerr
%29意味着发送到这些流对象的任何输出都会立即刷新到OS%28viastd::basic_ostream::sentry
%27 s析构函数%29。
此外,std::cerr.tie()
回报&
std::cout
28%wcerr
和wcout
%29,意味着在std::cerr
先执行std::cout
.flush()
%28std::basic_ostream::sentry
%27s构造函数%29%28自C++11%29。
注记
名称中的%27c%27引用“字符”%28stroustrup.com常见问题29%;cerr
表示“字符错误%28流%29”和wcerr
意思是“宽字符错误%28流%29”
例
通过cerr输出到stderr会在cout上刷新挂起的输出,而通过阻塞输出到stderr则不会。
二次
#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()
二次
另见
Init | initializes standard stream objects (public member class of std::ios_base) |
---|---|
clogwclog | writes to the standard C error stream stderr(global object) |
coutwcout | writes to the standard C output stream stdout(global object) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。