ostream& operator<< (ostream&, bool)

B. James Phillippe bryan@terran.org
Sun Apr 12 23:04:00 GMT 1998


On Mon, 13 Apr 1998, Gabriel Dos Reis wrote:

>     cout << (1 == 1) << '\n' << (1 == 2) << '\n';
> 
> shouldn't the output be looks like 
> 
> true
> false
> 
> instead of
> 
> 1
> 0

0 *is* false and 1 *is* true, as far as the machine is concerned.  Printing
a character string (eg., "true" or "false") is not the same thing as
printing an integral evaluation; it's equivalent to expecting the string
"five" or "nineteen" to be used in place of the values '5' and '19'.  There
are ways to accomplish what you're trying to do without redefining C++.
One method comes to mind:

inline const char* boolToString( bool expr ) {
	
	return (expr == true) ? "true" : "false";
}

cheers,
-bp
--
B. James Phillippe <bryan@terran.org>
Linux Software Engineer, WGT Inc.
http://earth.terran.org/~bryan




More information about the Gcc mailing list