C++ std::ios::tie() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, tie() connects a stream to an output stream. Before every input or output on the first stream, the tied output stream is flushed first.

That is why a prompt written with cout appears before cin waits for you: std::cin is tied to std::cout by default. Include <iostream> to use it.

This is a member function of std::ios. You call it with a dot on a stream object, for example std::cin, std::cout, a file stream, or a std::stringstream. A stream is simply C++'s way to read or write data one piece after another.

Syntax

Call tie with no argument to get the tied stream. Pass a pointer to set a new one.

C++
std::ostream* t = std::cin.tie();     // get
std::cin.tie(nullptr);                // untie
std::cin.tie(&std::cout);             // tie again
CallWhat it does
std::cin.tie()Returns a pointer to the tied stream, or nullptr.
std::cin.tie(nullptr)Removes the tie. cin no longer flushes cout.
in.tie(&out)Ties in to out and returns the old tied stream.

Untie only in programs that care about speed, and make sure the prompts are still flushed some other way.

Library declarations
C++
ostream* tie() const;
ostream* tie(ostream* tiestr);

Parameters

ParameterDescription
tiestrOptional. A pointer to the output stream to tie, or nullptr for no tie. Without it, tie() only returns the current value.

Return Value

Return ValueDescription
std::ostream*A pointer to the stream that was tied before the call, or nullptr if there was none.

C++ Examples

Example 1 — cin Is Tied to cout by Default

Ask cin.tie() which stream it is linked to.

C++
#include <iostream>

int main() {
    std::ostream* t = std::cin.tie();

    std::cout << "cin is tied to cout: " << (t == &std::cout ? "yes" : "no") << '\n';
    std::cout << "cout is tied to something: " << (std::cout.tie() ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. std::cin.tie() returns a pointer. The program compares it with the address of std::cout.
  2. They match, so cin is tied to cout: every read from the keyboard flushes cout first.
  3. cout itself is not tied to anything, so its tie() is nullptr, which counts as false.

Example 2 — Untie cin for Speed

cin.tie(nullptr) removes the link and returns the old stream, so you can put it back.

C++
#include <iostream>

int main() {
    std::ostream* old = std::cin.tie(nullptr);

    std::cout << "tied now: " << (std::cin.tie() ? "yes" : "no") << '\n';

    std::cin.tie(old);
    std::cout << "tied again: " << (std::cin.tie() ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. tie(nullptr) removes the link. It returns the stream that was tied, and the program stores it in old.
  2. Now std::cin.tie() is nullptr, so the first line prints no.
  3. std::cin.tie(old) restores the link, so the second line prints yes.

Example 3 — Tie Two Streams of Your Own

Any input stream can be tied to any output stream. Here both are string streams.

C++
#include <iostream>
#include <sstream>

int main() {
    std::ostringstream log;
    std::istringstream in("5");

    std::ostream* before = in.tie(&log);

    std::cout << "was tied before: " << (before ? "yes" : "no") << '\n';
    std::cout << "tied to log: " << (in.tie() == &log ? "yes" : "no") << '\n';
    return 0;
}

How It Works

  1. A new string stream has no tie, so in.tie(&log) returns nullptr.
  2. The first line prints no because that returned pointer is empty.
  3. in.tie() now returns the address of log, so log will be flushed before each input on in.

Frequently Asked Questions

It returns the output stream that is tied to cin. By default that is cout, so cout is flushed before each read from cin.
It removes the tie, so reading from cin no longer flushes cout. Together with sync_with_stdio(false) it makes input faster.
Without the tie, cout is not flushed before cin waits. Write endl or << std::flush after the prompt.
The pointer that was tied before the call, so you can restore it later.
Yes, an input or output stream can be tied to one output stream. A stream can be tied to only one at a time.

Next: unget()

Step back one character in the input stream.

unget() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful