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);
獲取/設定繫結的流
第一個形式(1)返回一個指向已繫結輸出流的指標。

第二個形式(2)將物件與tiestr繫結,並返回呼叫前繫結的流的指標(如果之前有繫結)。

繫結的流是一個輸出流物件,在對此流物件進行每次 i/o 操作之前會被重新整理

預設情況下,cin 繫結到 coutwcin 繫結到 wcout。庫的實現可能會在初始化時繫結其他標準流。
預設情況下,標準的窄字元流 cincerr 繫結到 cout,而它們對應的寬字元流(wcinwcerr)則繫結到 wcout。庫的實現也可能將 clogwclog 繫結。

引數

tiestr
一個輸出流物件。
char_typetraits_type 是成員型別,分別定義為第一個和第二個類模板引數的別名(參見 basic_ios types)。

返回值

指向呼叫前繫結的流物件的指標,如果流未繫結,則為空指標

示例

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

輸出

tie example:
This is inserted into cout


資料競爭

訪問(1)或修改(2)流物件。
併發訪問同一個流物件可能導致資料爭用。

異常安全

基本保證:如果丟擲異常,流處於有效狀態。