Topic: bool again (was: 'null' keyword)
Author: "Wil Evers" <WEvers@pcl-software.nl>
Date: 1997/09/01 Raw View
Paul D. DeRocco <pderocco@ix.netcom.crud.com> wrote in article
<340794A6.198B@ix.netcom.crud.com>...
> Wil Evers wrote:
> >
> > As you say, 'you logically need to able to overload functions on a
> > boolean type'. On the other hand, I've never come across a real-life
> > example where overloading on bool made any sense.
>
> How about ostream::operator<<(bool)? I don't know what it actually
> outputs in the current draft standard, but I could imagine preferring it
> to output "false" or "true", rather than "0" or "1".
But of course! This one's so obvious I didn't think about it :-).
According to the draft, what happens when you stream out a bool depends on
the boolalpha flag which is defined in class ios_base. If this flag is
set, the actual string that is printed depends on the locale.
- Wil
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: David R Tribble <david.tribble@central.beasys.com>
Date: 1997/08/29 Raw View
"Wil Evers" <WEvers@pcl-software.nl> wrote:
> As you say, 'you logically need to able to overload functions on a boolean
> type'. On the other hand, I've never come across a real-life example where
> overloading on bool made any sense.
>
> Out of historical interest, does anyone know if any such examples were
> presented to the committee? If so, what were they?
It's a safe bet that if it's possible, someone out there has done it or
has found a need for it.
Here's a contrived, but still practical, example:
Class TypeNames
{
public:
static const char * toString(int x); // Type name for "int"
static const char * toString(char x); // Type name for "char"
static const char * toString(float x); // Type name for "float"
static const char * toString(bool x); // etc...
...
};
--------------------. BEA Systems, Inc. ,-. +1-972-738-6125 Office
David R. Tribble \ ,------------------' \ +1-972-738-6111 Fax
http://www.beasys.com `-' Dallas, TX 75248 `-----------------------
david.tribble@noSPAM.beasys.com http://www.flash.net/~dtribble
-------------------------------------------------------------------------
Send junk to: postmaster@agis.net mrchig@easynet.co.uk
emailer@qlink2info.com simrem@answerme.com office@canma.dyn.ml.org
markdan3@earthlink.net clover@earthfriends.com office@savetrees.com
abuse@cyberpromo.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: tony@online.tmx.com.au (Tony Cook)
Date: 1997/08/31 Raw View
Wil Evers (WEvers@pcl-software.nl) wrote:
: As you say, 'you logically need to able to overload functions on a boolean
: type'. On the other hand, I've never come across a real-life example where
: overloading on bool made any sense.
The most obvious one:
// this should really use template <class charT, class traits>
// basic_ostream<charT, traits>
ostream &operator <<(ostream &out, bool value)
{
return out << (value ? "true" : "false");
}
--
Tony Cook - tony@online.tmx.com.au <= testing .procmailrc
100237.3425@compuserve.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Paul D. DeRocco" <pderocco@ix.netcom.crud.com>
Date: 1997/08/31 Raw View
Wil Evers wrote:
>
> As you say, 'you logically need to able to overload functions on a boolean
> type'. On the other hand, I've never come across a real-life example where
> overloading on bool made any sense.
How about ostream::operator<<(bool)? I don't know what it actually
outputs in the current draft standard, but I could imagine preferring it
to output "false" or "true", rather than "0" or "1".
--
Ciao,
Paul
(Please remove the extra "crud" from the return address,
which has been altered to foil junk mail senders.)
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/08/31 Raw View
Wil Evers <WEvers@pcl-software.nl> writes:
> As you say, 'you logically need to able to overload functions on a boolean
> type'. On the other hand, I've never come across a real-life example where
> overloading on bool made any sense.
>
> Out of historical interest, does anyone know if any such examples were
> presented to the committee? If so, what were they?
I don't know but I can imagine one:
cout << (a < b);
--
Valentin Bonnard
mailto:bonnardv@pratique.fr
http://www.pratique.fr/~bonnardv (Informations sur le C++ en Francais)
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Barry Margolin <barmar@bbnplanet.com>
Date: 1997/08/31 Raw View
In article <2.2.32.19970829220844.00335d64@central.beasys.com>,
David R Tribble <david.tribble@central.beasys.com> wrote:
>Here's a contrived, but still practical, example:
> Class TypeNames
> {
> public:
> static const char * toString(int x); // Type name for "int"
> static const char * toString(char x); // Type name for "char"
> static const char * toString(float x); // Type name for "float"
> static const char * toString(bool x); // etc...
> ...
> };
This example is more than just contrived, it's circular reasoning. The
only reason this example needs to distinguish int from bool is because
they're distinct types. But he was asking for an example of why you need
them to be distinct types!
--
Barry Margolin, barmar@bbnplanet.com
BBN Corporation, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]