title: How is `std::cout` implemented?
body: <p><code>std::cout</code> is an instance of <code>std::ostream</code>. I can see the declaration of <code>std::cout</code> in a file named <code>/usr/include/c++/7/iostream</code>:</p>
  <pre><code>extern ostream cout;      /// Linked to standard output
  </code></pre>
  <p>And <code>std::ostream</code> is defined by <code>typedef std::basic_ostream&lt;char&gt; std::ostream</code>.</p>
  <p>What's more, it seems that you can't create an instance of <code>std::ostream</code>. See this demo <a href="https://godbolt.org/z/qKn6Ya9cP" rel="noreferrer">code snippet</a>:</p>
  <pre><code>#include&lt;iostream&gt;
  
  int main()
  {
      std::ostream os;
      return 0;
  }
  </code></pre>
  <p>Here is what the compiler complains about the code snippet above:</p>
  <pre><code>In file included from /opt/compiler-explorer/gcc-4.9.0/include/c++/4.9.0/iostream:39:0,
                   from &lt;source&gt;:1:
  /opt/compiler-explorer/gcc-4.9.0/include/c++/4.9.0/ostream: In function 'int main()':
  /opt/compiler-explorer/gcc-4.9.0/include/c++/4.9.0/ostream:384:7: error: 'std::basic_ostream&lt;_CharT, _Traits&gt;::basic_ostream() [with _CharT = char; _Traits = std::char_traits&lt;char&gt;]' is protected
         basic_ostream()
         ^
  &lt;source&gt;:5:18: error: within this context
       std::ostream os;
                    ^
  </code></pre>
  <p>The question arises, since the <code>std::basic_ostream&lt;_CharT, _Traits&gt;::basic_ostream()</code> is marked protected, how <code>std::cout</code> is created?</p>
  <p>This <a href="https://en.cppreference.com/w/cpp/io/cout" rel="noreferrer">link</a> on CppReference seems not very meaningful. It does not clearly tell me how <code>std::cout</code> is implemented and how <code>std::cout</code> is created by the constructor of <code>std::ostream</code>. As far as I can see, the most related information is:</p>
  <blockquote>
  <p>The global objects <code>std::cout</code> and <code>std::wcout</code> control output to a stream buffer of implementation-defined type (derived from <code>std::streambuf</code>), associated with the standard C output stream <code>stdout</code>.</p>
  </blockquote>
  <p>And nothing more.</p>
  <p>I am working on <code>Ubuntu</code> with <code>gcc 4.9</code></p>
  <p>Thanks to @NathanPierson.</p>
  <p>He told me that</p>
  <blockquote>
  <p><code>std::basic_ostream</code> has a constructor that takes a pointer to a <code>std::basic_streambuf</code> object.<code>std::cout</code> is initialized using a pointer to an instance of some implementation-defined derived class of <code>std::basic_streambuf</code>.</p>
  </blockquote>
  <p>, which moves me closer to the answer.</p>