Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::basic_ios<CharT,Traits>::copyfmt

      From cppreference.com
      <cpp‎ |io‎ |basic ios
       
       
       
       
      basic_ios& copyfmt(const basic_ios& other);

      Ifother refers to the same object as*this, has no effects. Otherwise, copies the state of the streamother into*this. This is done in the following sequence:

      1) Calls every callback registered byregister_callback() passingerase_event as parameter.
      2) Copies all member objects fromother to*this except forrdstate(), the exception mask, andrdbuf(). In particular, makes copies of the locale, the formatting flags, the contents of the arraysstd::ios_base::iword andstd::ios_base::pword (but not theiword andpword pointers themselves), the callbacks, and the tied stream.
      3) Calls every callback registered byregister_callback() passingcopyfmt_event as parameter.
      4) Copies the exception mask fromother to*this as if by callingexceptions(other.exceptions()).

      Contents

      [edit]Parameters

      other - another stream to use as source

      [edit]Return value

      *this

      [edit]Notes

      The second pass through the callbacks may be used to deep-copy the user-defined objects pointed to by the pointers instd::ios_base::pword.

      copyfmt() may be used to save and restore the state of a stream. Boost provides a more fine-grainedI/O state savers library for the same purpose.

      [edit]Example

      Makes thestd::ofstream object "out" behave exactly likestd::cout, including formatting,tie() tostd::cin, etc.

      Run this code
      #include <bitset>#include <climits>#include <fstream>#include <iostream> int main(){std::ofstream out;     out.copyfmt(std::cout);// copy everything except rdstate and rdbuf    out.clear(std::cout.rdstate());// copy rdstate    out.basic_ios<char>::rdbuf(std::cout.rdbuf());// share the buffer     out<<"Hello, world\n"; auto bin=[](std::ios_base::fmtflags f){returnstd::bitset<sizeof(std::ios_base::fmtflags)*CHAR_BIT>{static_cast<unsignedlonglong>(f)};};std::ofstream out2;std::cout<<"1) out2.flags(): "<< bin(out2.flags())<<'\n';std::cout<<"2) cout.flags(): "<< bin(std::cout.flags())<<'\n';std::cout.setf(std::ios::hex| std::ios::fixed| std::ios::boolalpha);std::cout<<"3) cout.flags(): "<< bin(std::cout.flags())<<'\n';    out2.copyfmt(std::cout);// copy everything except rdstate and rdbufstd::cout<<"4) out2.flags(): "<< bin(out2.flags())<<'\n';}

      Possible output:

      Hello, world1) out2.flags(): 000000000000000000010000000000102) cout.flags(): 000000000000000000010000000000103) cout.flags(): 000000000000000000010000000011114) out2.flags(): 00000000000000000001000000001111

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 256C++98step 3 called the registered callbacks with the
      event typecopy_event, which is not defined
      corrected to
      copyfmt_event
      LWG 292C++98ifother refers to the same object as*this, the member objects
      were still copied and the registered callbacks were still called
      do nothing
      in this case
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_ios/copyfmt&oldid=158804"

      [8]ページ先頭

      ©2009-2025 Movatter.jp