CKTIS / C++
std::basic_ios::tie — tying a stream to an output stream
std::basic_ios::tie queries or changes a tied output stream. The tie allows its buffer to be flushed before input; nullptr removes the tie, and the setter overload returns the previous pointer.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
std::basic_ostream<charT, traits>* tie() const;
std::basic_ostream<charT, traits>* tie(std::basic_ostream<charT, traits>* tiestr);std::basic_ios::tie has two overloads. tie() returns the tied output stream pointer or nullptr. tie(tiestr) sets the pointer and returns the previous value, not the new one. Both streams must have matching charT and traits types.
When an input stream is in a good state, its input-preparation sentry object calls flush() on the tied output. The standard permits suppressing this call when the put area is empty and deferring it until underflow(); if that call never occurs, the flush may be eliminated entirely.
By default, std::cin is tied to std::cout, helping display a prompt before waiting for input. std::cin.tie(nullptr) removes this tie; you must then explicitly arrange any necessary flushing. Merely setting a tie does not call flush().
Do not create self-ties or indirect cycles: they violate the precondition and lead to undefined behavior. The tied object must remain alive while its pointer is used. The example checks attachment, the return value, and restoration without interactive input.
Examples
#include <iostream>
#include <sstream>
int main() {
std::ostringstream output;
std::istringstream input("42");
auto* previous = input.tie(&output);
const bool attached = input.tie() == &output;
output << "ready";
int value = 0;
input >> value;
auto* removed = input.tie(previous);
std::cout << std::boolalpha
<< attached << '\n'
<< (removed == &output) << '\n'
<< value << '\n';
}
Result
true true 42
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.