Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |io‎ |basic ios
       
       
       
       
      std::basic_ostream<CharT, Traits>* tie()const;
      (1)
      std::basic_ostream<CharT, Traits>* tie(std::basic_ostream<CharT, Traits>* str);
      (2)

      Manages the tied stream. A tied stream is an output stream which is synchronized with the sequence controlled by the stream buffer (rdbuf()), that is,flush() is called on the tied stream before any input/output operation on*this.

      1) Returns the current tied stream. If there is no tied stream, a null pointer is returned.
      2) Sets the current tied stream tostr. Returns the tied stream before the operation. If there is no tied stream, a null pointer is returned. Ifstr is not null andtie() is reachable by traversing the linked list of tied stream objects starting fromstr->tie(), the behavior is undefined.

      Contents

      [edit]Parameters

      str - an output stream to set as the tied stream

      [edit]Return value

      The tied stream, or a null pointer if there was no tied stream.

      [edit]Exceptions

      May throw implementation-defined exceptions.

      [edit]Notes

      By default, the standard streamstd::cout is tied tostd::cin andstd::cerr. Similarly, its wide counterpartstd::wcout is tied tostd::wcin andstd::wcerr.

      [edit]Example

      Run this code
      #include <fstream>#include <iomanip>#include <iostream>#include <string> int main(){std::ofstream os("test.txt");std::ifstream is("test.txt");std::string value("0");     os<<"Hello";    is>> value; std::cout<<"Result before tie(): "<<std::quoted(value)<<"\n";    is.clear();    is.tie(&os);     is>> value; std::cout<<"Result after tie(): "<<std::quoted(value)<<"\n";}

      Output:

      Result before tie(): "0"Result after tie(): "Hello"

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 835C++98two streams could be tied to each other[1]
      (either directly or through another intermediate stream object)
      the behavior is undefined in this case
      1. std::basic_ostream::flush() is anUnformattedOutputFunction, so it creates a sentry object while being called. Whenflush() is called on a stream object, theconstructor of the sentry object will callflush() on its tied stream, and thatflush() will construct another sentry object and its constructor will callflush() on the tied stream of that stream and so on. Therefore, if streamsa andb are (directly or indirectly) tied to each other, callinga.flush() will eventually callb.flush(), which will eventually calla.flush(), and will result in an infinite loop.
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/basic_ios/tie&oldid=149634"

      [8]ページ先頭

      ©2009-2025 Movatter.jp