6

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?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Tebe
  • 3,176
  • 8
  • 40
  • 60

3 Answers3

9

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.

Gavin Haynes
  • 1,721
  • 11
  • 21
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
7

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.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
Alan Stokes
  • 18,815
  • 3
  • 45
  • 64
  • well, nice as I see here http://www.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. – Tebe May 20 '12 at 19:23
  • @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. – Mooing Duck May 20 '12 at 19:25
  • 1
    @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. – Benjamin Lindley May 20 '12 at 19:26
  • 2
    @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). – Fred Foo May 20 '12 at 19:29
  • it's nice. But in I can find only this: extern ostream cout; ///< Linked to standard output – Tebe May 20 '12 at 19:29
  • 7
    @shbk: **FORGET ABOUT COUT. IT IS NOT WHAT YOU ARE LOOKING FOR. READ THE THINGS WE ARE SAYING.** – Benjamin Lindley May 20 '12 at 19:31
  • 1
    @Mooing it's not just that - the OP's link points to the SGI STL, which definitely doesn't include `cout`. – Alan Stokes May 20 '12 at 19:33
2

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

Benoît
  • 3,355
  • 2
  • 29
  • 34
  • Better: [gcc.gnu.org/git/?p=gcc.git&a=search&h=HEAD&st=grep&s=\bcout\b&sr=1](http://gcc.gnu.org/git/?p=gcc.git&a=search&h=HEAD&st=grep&s=\bcout\b&sr=1) – kennytm May 21 '12 at 17:37