public member function
<ios> <iostream>

std::basic_ios::tie

get (1)
basic_ostream<char_type,traits_type>* tie() const;
set (2)
basic_ostream<char_type,traits_type>* tie (basic_ostream<char_type,traits_type>* tiestr);
Get/set tied stream
The first form (1) returns a pointer to the tied output stream.

The second form (2) ties the object to tiestr and returns a pointer to the stream tied before the call, if any.

The tied stream is an output stream object which is flushed before each i/o operation in this stream object.

By default, cin is tied to cout, and wcin to wcout. Library implementations may tie other standard streams on initialization.
By default, the standard narrow streams cin and cerr are tied to cout, and their wide character counterparts (wcin and wcerr) to wcout. Library implementations may also tie clog and wclog.

Parameters

tiestr
An output stream object.
char_type and traits_type are member types defined as aliases of the first and second class template parameters, respectively (see basic_ios types).

Return Value

A pointer to the stream object tied before the call, or a null pointer in case the stream was not tied.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// redefine tied object
#include <iostream>     // std::ostream, std::cout, std::cin
#include <fstream>      // std::ofstream

int main () {
  std::ostream *prevstr;
  std::ofstream ofs;
  ofs.open ("test.txt");

  std::cout << "tie example:\n";

  *std::cin.tie() << "This is inserted into cout";
  prevstr = std::cin.tie (&ofs);
  *std::cin.tie() << "This is inserted into the file";
  std::cin.tie (prevstr);

  ofs.close();

  return 0;
}

Output:

tie example:
This is inserted into cout


Data races

Accesses (1) or modifies (2) the stream object.
Concurrent access to the same stream object may cause data races.

Exception safety

Basic guarantee: if an exception is thrown, the stream is in a valid state.