Take the 2-minute tour ×

This might be a beginner question and understanding how cout works is probably key here. If somebody could link to a good explanation, it would be great. cout<<cout and cout<<&cout print hex values separated by 4 on a linux x86 machine.

share|improve this question
Here is an example for people. ideone.com/0FZXZ –  Daniel A. White Sep 20 '11 at 17:21
What actually is the question? –  Puppy Sep 20 '11 at 17:21
Why are you asking this. The question does not make any sense their is no logic in doing that. –  Loki Astari Sep 20 '11 at 17:25
@Tux-D: Why I asked it: I saw this idiom somewhere and it confused me. I understood why cout<<&cout would print the address of the current instance but not what it meant when we print cout<<cout Regarding the logic in doing that: Left as an exercise for the reader? :P –  byslexia Sep 20 '11 at 20:06

5 Answers 5

up vote 11 down vote accepted

cout << cout is equivalent to cout << cout.operator void *(). This is the idiom used before C++11 to determine if an iostream is in a failure state, and is implemented in std::ios_base; it usually returns the address of static_cast<std::ios_base *>(&cout).

cout << &cout prints out the address of cout.

Since std::ios_base is a virtual base class of cout, it may not necessarily be contiguous with cout. That is why it prints a different address.

share|improve this answer

cout << cout is using the built-in conversion to void* that exists for boolean test purposes. For some uninteresting reason your implementation uses an address that is 4 bytes into the std::cout object. In C++11 this conversion was removed, and this should not compile.

cout << &cout is printing the address of the std::cout object.

share|improve this answer

cout << &cout is passing cout the address of cout.

cout << cout is printing the value of implicitly casting cout to a void* pointer using its operator void*. See here.

share|improve this answer

As already stated, cout << cout uses the void* conversion provided for bool testing (while (some_stream){ ... }, etc.)

It prints the value &cout + 4 because the conversion is done in the base implementation, and casts to its own type, this is from libstdc++:

operator void*() const
{ return this->fail() ? 0 : const_cast<basic_ios*>(this); }
share|improve this answer

cout<<&cout is passing the address of cout to the stream.

share|improve this answer

Your Answer


