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

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.

# Declarations

std::basic_ostream<CharT, Traits>* tie() const;
std::basic_ostream<CharT, Traits>* tie( std::basic_ostream<CharT, Traits>* str );

# Parameters

# Return value

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

# Notes

By default, the standard stream std::cout is tied to std::cin and std::cerr. Similarly, its wide counterpart std::wcout is tied to std::wcin and std::wcerr.

# Example

#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";
}

# Defect reports

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