c++ STL cout source code

11,086

Solution 1

cout is not part of the STL, so you won't find the source for cout in the STL source.

You probably want to look for the source for your C++ standard library, which was based on the STL, but also contains iostreams. Where that is depends on what platform you're using.

Solution 2

If you happen to be using GCC, then libstdc++ is your C++ library. Its sources can be found on gcc.gnu.org. cout is defined on line 58 of src/c++98/globals_io.cc.

Solution 3

std::cout is not a function, it is a instance of std::ostream (interface description) that is tied to standard output.

If you are using gcc/libstdc++, have a blast browsing its source code online

Share:
11,086

Related videos on Youtube

Tebe
Author by

Tebe

programmer and human languages-fancier I prefer Bitbucket over Github, you can find my projects here https://bitbucket.org/gekannt/

Updated on June 04, 2022

Comments

  • Tebe
    Tebe almost 2 years

    I want to see source code of STL std::cout function. I looked at iostream, but I've seen only "extern cout". So, I guess that it's defined somewhere in the library.

    I downloaded source code from official site

    I extracted it and did:

    sh@sh-R528-R728:~/desktop/stl$ grep -F * | grep "cout"
    

    but I got nothing.

    What am I doing wrong? Where is the source code?

    • chris
      chris almost 12 years
      I'm willing to bet the source code would burn your eyes.
    • Xeo
      Xeo almost 12 years
      std::cout is not a function, it's an object. That aside, try your luck with libc++ in iostream.cpp.
    • Benjamin Lindley
      Benjamin Lindley almost 12 years
      operator<< is what you are probably interested in. Search for that.
    • Alan Stokes
      Alan Stokes almost 12 years
      std::cout is also not part of the STL.
    • Bo Persson
      Bo Persson almost 12 years
      @shbk - Most of the source is templates that you can find in the headers. std::cout is just an object of type ostream. You will find all of that in <ostream> provided with your compiler.
  • Tebe
    Tebe almost 12 years
    well, nice as I see here cplusplus.com/reference . The header of cout is ostream. I cannot find realization of cout in ostream. Could you plz give a tip how to find it? I sought word "cout" and I've founf it only in comments.
  • Mooing Duck
    Mooing Duck almost 12 years
    @shbk: Alan is referring to the fact that the C++ standard library and the STL are different things. However, many many people call the C++ standard library "STL" since it originated as a copy of the STL Library. The original STL library did not contain streams.
  • Benjamin Lindley
    Benjamin Lindley almost 12 years
    @shbk: cout should not be in ostream, it should be in iostream. But as was said in the comments, cout is an object, not a function. So finding cout isn't going to tell you how it works. You need to find its class(ostream) and the functions of that class.
  • Fred Foo
    Fred Foo almost 12 years
    @MooingDuck: the C++ standard library did not originate as a copy of the STL; rather, it subsumed large parts of the STL (but not all of it, even in C++11).
  • Tebe
    Tebe almost 12 years
    it's nice. But in <iostream> I can find only this: extern ostream cout; ///< Linked to standard output
  • Benjamin Lindley
    Benjamin Lindley almost 12 years
    @shbk: FORGET ABOUT COUT. IT IS NOT WHAT YOU ARE LOOKING FOR. READ THE THINGS WE ARE SAYING.
  • Alan Stokes
    Alan Stokes almost 12 years
    @Mooing it's not just that - the OP's link points to the SGI STL, which definitely doesn't include cout.
  • kennytm
    kennytm almost 12 years