public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* ostream& operator<< (ostream&, bool)
@ 1998-04-12 16:50 Gabriel Dos Reis
  1998-04-12 21:51 ` Alexandre Oliva
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Gabriel Dos Reis @ 1998-04-12 16:50 UTC (permalink / raw)
  To: egcs

Consider:

#include <iostream>

int main()
{
    cout << (1 == 1) << '\n' << (1 == 2) << '\n';
}

shouldn't the output be looks like 

true
false

instead of

1
0

?
IMHO, since bool type became a builtin type distinct form int, objects
of type bool should be displayed as they are (ie. true, false), and
not coerced to int before output. 

-- Gaby
"One reason that life is complex is that it has a 
real part and imaginary part." -- Andrew Koenig

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ostream& operator<< (ostream&, bool)
  1998-04-12 16:50 ostream& operator<< (ostream&, bool) Gabriel Dos Reis
@ 1998-04-12 21:51 ` Alexandre Oliva
  1998-04-12 23:04 ` B. James Phillippe
       [not found] ` <orogy6s4zg.fsf.cygnus.egcs@zecarneiro.lsd.dcc.unicamp.br>
  2 siblings, 0 replies; 5+ messages in thread
From: Alexandre Oliva @ 1998-04-12 21:51 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: egcs

Gabriel Dos Reis writes:

> shouldn't the output be looks like 

> true
> false

> instead of

> 1
> 0

Not actually.  This is controlled by the std::boolalpha ios
manipulator, which is not implemented yet.  The default behavior is to 
use the numeric format.  In order to get the alphanumeric boolean
representation, you'd have to use:

cout << boolalpha << true << endl << false << endl;

-- 
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ostream& operator<< (ostream&, bool)
  1998-04-12 16:50 ostream& operator<< (ostream&, bool) Gabriel Dos Reis
  1998-04-12 21:51 ` Alexandre Oliva
@ 1998-04-12 23:04 ` B. James Phillippe
  1998-04-13  3:22   ` Gabriel Dos Reis
       [not found] ` <orogy6s4zg.fsf.cygnus.egcs@zecarneiro.lsd.dcc.unicamp.br>
  2 siblings, 1 reply; 5+ messages in thread
From: B. James Phillippe @ 1998-04-12 23:04 UTC (permalink / raw)
  To: Gabriel Dos Reis; +Cc: egcs

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ostream& operator<< (ostream&, bool)
  1998-04-12 23:04 ` B. James Phillippe
@ 1998-04-13  3:22   ` Gabriel Dos Reis
  0 siblings, 0 replies; 5+ messages in thread
From: Gabriel Dos Reis @ 1998-04-13  3:22 UTC (permalink / raw)
  To: B. James Phillippe; +Cc: Gabriel Dos Reis, egcs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1249 bytes --]

>>>>> «BJP», B James Phillippe <bryan@terran.org> écrit :

BJP> 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

BJP> 0 *is* false and 1 *is* true, as far as the machine is concerned.  Printing

Well, I'm not concerned with the machine representation... With the
same reasonning 
	cout << 5;
should output 101 by *default* instead of 5.

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

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

not necessary: Alexandre gave me the right answer.

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

-- Gaby
"One reason that life is complex is that it has a 
real part and imaginary part." -- Andrew Koenig

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: ostream& operator<< (ostream&, bool)
       [not found] ` <orogy6s4zg.fsf.cygnus.egcs@zecarneiro.lsd.dcc.unicamp.br>
@ 1998-04-13 15:57   ` Nathan Myers
  0 siblings, 0 replies; 5+ messages in thread
From: Nathan Myers @ 1998-04-13 15:57 UTC (permalink / raw)
  To: egcs

Alexandre Oliva wrote:
> 
> Gabriel Dos Reis writes:
> 
> > shouldn't the output be looks like
> 
> > true
> > false
> 
> > instead of
> 
> > 1
> > 0
> 
> Not actually.  This is controlled by the std::boolalpha ios
> manipulator, which is not implemented yet.  The default behavior is to
> use the numeric format.  In order to get the alphanumeric boolean
> representation, you'd have to use:
> 
> cout << boolalpha << true << endl << false << endl;

Incidentally, the Draft is broken as regards

  bool b;
  cin >> boolalpha >> b;

It's quite embarrassing, but the code above has indeterminate results 
because the Draft says if b (going in) is true, >> will try to match
"true", or if false, then "false", and report an error otherwise.  
In other words, you have to know what you're about to read before you
read it.  There will be a defect report about this.

Nathan Myers
ncm@cygnus.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~1998-04-13 15:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-04-12 16:50 ostream& operator<< (ostream&, bool) Gabriel Dos Reis
1998-04-12 21:51 ` Alexandre Oliva
1998-04-12 23:04 ` B. James Phillippe
1998-04-13  3:22   ` Gabriel Dos Reis
     [not found] ` <orogy6s4zg.fsf.cygnus.egcs@zecarneiro.lsd.dcc.unicamp.br>
1998-04-13 15:57   ` Nathan Myers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).