std::cerr, std::wcerr
ヘッダ <iostream> で定義
|
||
extern std::ostream cerr; |
(1) | |
extern std::wostream wcerr; |
(2) | |
グローバルオブジェクト std::cerr
および std::wcerr
は標準 C エラー出力ストリーム stderr に紐付けられている処理系定義の (それぞれ std::streambuf および std::wstreambuf から派生した) 型のストリームバッファへの出力を制御します。
これらのオブジェクトは std::ios_base::Init 型のオブジェクトが最初に構築されるときまたはそれより前に初期化されることが保証されており、順序付き初期化を行う静的オブジェクトのコンストラクタおよびデストラクタ内で使用することができます (そのオブジェクトが定義されるより前に <iostream>
��インクルードされている限り)。
sync_with_stdio(false) が行われていなければ、書式付き出力と書式なし出力のどちらについても、これらのオブジェクトに複数のスレッドから並行的にアクセスしても安全です。
いったん初期化されると、 (std::cerr.flags() & unitbuf) != 0 になります (wcerr
についても同様です)。 これは、これらのストリームオブジェクトに送られたあらゆる出力が直ちに (std::basic_ostream::sentry のデストラクタを通して) OS にフラッシュされることを意味します。
さらに、 std::cerr.tie() は &std::cout を返します ( |
(C++11以上) |
[編集] ノート
名前の中の「c」は「character」を表します (stroustrup.com FAQ)。 cerr
は「character error (stream)」、 wcerr
は「wide character error (stream)」という意味です。
[編集] 例
cerr を通した stderr への出力は cout の保留中の出力をフラッシュしますが、 clog を通した 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()
[編集] 関連項目
標準ストリームオブジェクトを初期化します ( std::ios_base のパブリックメンバクラス)
| |
標準 C エラーストリーム stderr に書き込みます (グローバルオブジェクト) | |
標準 C 出力ストリーム stdout に書き込みます (グローバルオブジェクト) | |
入力ストリームに関連付けられた FILE* 型の式 出力ストリームに関連付けられた FILE* 型の式 エラー出力ストリームに関連付けられた FILE* 型の式 (マクロ定数) |