c++ STL cout source code
Asked Answered
A

3

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?

Any answered 20/5, 2012 at 19:7 Comment(10)
I'm willing to bet the source code would burn your eyes.Gide
may be, I just want to see how it works. I would to like to compare printf and cout inside.Any
std::cout is not a function, it's an object. That aside, try your luck with libc++ in iostream.cpp.Maisiemaison
operator<< is what you are probably interested in. Search for that.Cotta
std::cout is also not part of the STL.Righthander
or probably you might be interested in looking out for ostream.K
@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.Thorley
Also note this from the SGI page: "Last updated: 8-June-00".Thorley
it's sad for me, but there is only one entry of cout in ostream file - in comments. I am confused. I feel that I only go around.Any
std::cout is not a function; it is an instance of std::ostream. It is not part of the SGI STL, but of the C++ Standard Library. This is why everybody calling [parts of] the Standard Library "the STL" is harmful.Vanhouten
R
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.

Righthander answered 20/5, 2012 at 19:13 Comment(7)
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.Any
@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.Launch
@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.Cotta
@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).Hyetography
it's nice. But in <iostream> I can find only this: extern ostream cout; ///< Linked to standard outputAny
@shbk: FORGET ABOUT COUT. IT IS NOT WHAT YOU ARE LOOKING FOR. READ THE THINGS WE ARE SAYING.Cotta
@Mooing it's not just that - the OP's link points to the SGI STL, which definitely doesn't include cout.Righthander
H
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.

Hyetography answered 20/5, 2012 at 19:31 Comment(0)
R
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.

Righthander answered 20/5, 2012 at 19:13 Comment(7)
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.Any
@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.Launch
@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.Cotta
@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).Hyetography
it's nice. But in <iostream> I can find only this: extern ostream cout; ///< Linked to standard outputAny
@shbk: FORGET ABOUT COUT. IT IS NOT WHAT YOU ARE LOOKING FOR. READ THE THINGS WE ARE SAYING.Cotta
@Mooing it's not just that - the OP's link points to the SGI STL, which definitely doesn't include cout.Righthander
V
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

Viridian answered 20/5, 2012 at 19:34 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.