Sending std::cout to std::cout

Sending std::cout to std::cout

Post by saurabh297 » Sat, 13 Jun 2009 08:33:41


I have a statement I'd like you to consider :

std::cout<<(std::cout<<"Hi");

that prints a Hii followed by some address.

The (std::cout<<"Hi") would be returning an object of type ostream,
but what is this address that is printed ?? How??

--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
 
 
 

Sending std::cout to std::cout

Post by Ulrich Eck » Sat, 13 Jun 2009 15:45:09


Up front: just step into it with a de *** .

Explanation: the conversion to a void pointer allows the use of a stream in
a conditional expression, typically seen like this:

if(!(in >> value))
error("failed to read value");

Using a void pointer instead of a bool makes it less likely to activate
unwanted implicit conversions. Note that this is still far from perfect,
using a pointer to a memberfunction is better still.

Uli

--
Sator Laser GmbH
Geschftsfhrer: Thorsten Fcking, Amtsgericht Hamburg HR B62 932


[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 
 
 

Sending std::cout to std::cout

Post by CornedBe » Sat, 13 Jun 2009 15:49:07


std::ostream has an implicit conversion to void* as an early (not
really safe) incarnation of the safe-bool idiom. You cannot insert an
ostream into an ostream (that doesn't make sense), but the compiler
will accept it by using the implicit conversion and printing that
address. What the address actually points to is unspecified, as it's
only meant to be interepreted as null/non-null. Chances are that most
implementations return this.

Sebastian


--
[ See http://www.yqcomputer.com/ ]
[ comp.lang.c++.moderated. First time posters: Do this! ]