Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::cerr,std::wcerr

      From cppreference.com
      <cpp‎ |io
       
       
       
       
      Defined in header<iostream>
      externstd::ostream cerr;
      (1)
      externstd::wostream wcerr;
      (2)

      The global objectsstd::cerr andstd::wcerr control output to a stream buffer of implementation-defined type (derived fromstd::streambuf andstd::wstreambuf, respectively), associated with the standard C error output streamstderr.

      These objects are guaranteed to be initialized during or before the first time an object of typestd::ios_base::Init is constructed and are available for use in the constructors and destructors of static objects withordered initialization (as long as<iostream> is included before the object is defined).

      Unlessstd::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.

      Once initialized,(std::cerr.flags()& unitbuf)!=0 (same forstd::wcerr) meaning that any output sent to these stream objects is immediately flushed to the OS (viastd::basic_ostream::sentry's destructor).

      In addition,std::cerr.tie() returns&std::cout (same forstd::wcerr andstd::wcout), meaning that any output operation onstd::cerr first executesstd::cout.flush() (viastd::basic_ostream::sentry's constructor).

      Contents

      [edit]Notes

      The 'c' in the name refers to "character" (stroustrup.com FAQ);cerr means "character error (stream)" andwcerr means "wide character error (stream)".

      [edit]Example

      Output tostderr viastd::cerr flushes out the pending output onstd::cout, while output tostderr viastd::clog does not.

      Run this code
      #include <chrono>#include <iostream>#include <thread>usingnamespace 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 coutOutput from thread...This output is tie()'d to cout...thread calls flush()

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 455C++98std::cerr.tie() and
      std::wcerr.tie() returned null pointers
      they return&std::cout and
      &std::wcout respectively

      [edit]See also

      initializes standard stream objects
      (public member class ofstd::ios_base)[edit]
      writes to the standard C error streamstderr
      (global object)[edit]
      writes to the standard C output streamstdout
      (global object)[edit]
      expression of typeFILE* associated with the input stream
      expression of typeFILE* associated with the output stream
      expression of typeFILE* associated with the error output stream
      (macro constant)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/cerr&oldid=181466"

      [8]ページ先頭

      ©2009-2025 Movatter.jp