Article View: comp.std.c++
Article #1545Re: What!!! true and false not identifiers?
From: Steve Clamage
Date: Wed, 02 Aug 1995 16:10
Date: Wed, 02 Aug 1995 16:10
37 lines
1918 bytes
1918 bytes
In article fsf@oculus.aifh.ed.ac.uk, andrewfg@oculus.aifh.ed.ac.uk (Andrew Fitzgibbon) writes:
>
>I don't believe it! true and false are keywords, at least according to
>g++, which gets a parse error on this code.
>
> f() {
> int true = 0; // parse error
> }
That is correct. C++ now has a built-in boolean type, and has added the
reserved words 'bool', 'true', and 'false'. Some compilers have already
implemented the boolean type. Eventually all compilers will.
For details of type bool, see the draft standard, available in postscript and
html on several ftp sites. Briefly: type bool has an implementation-defined
size, allows only the values 'true' and 'false'. 'true' converts implicitly in
expressions to the value 1, 'false' to 0. The value 0 can be converted
implicitly to bool and becomes 'false', any other value becomes 'true'.
Since bool is a distinct type, you can overload functions on type bool.
For compatibility with existing code during the transition period, you can
place something like the following in a project header file:
#ifdef NO_BOOL_TYPE
typedef int bool; // allows implicit conversions but not overloading
const bool false = 0;
const bool true = 1;
#endif
and then #define NO_BOOL_TYPE for compilers which do not yet support bool.
An alternative, which does not allow implicit conversions but which does
allow overloading on bool would be
enum bool { false, true };
---
Steve Clamage, stephen.clamage@eng.sun.com
Technical Details
Message-ID:
<3vo825$k9n@engnews2.Eng.Sun.COM>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!usenetReferences:
<fwv3ffkzm98.fsf@oculus.aifh.ed.ac.uk>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!usenet From: clamage@Eng.Sun.COM (Steve Clamage) Newsgroups: comp.std.c++ Subject: Re: What!!! true and false not identifiers? Date: 2 Aug 1995 16:10:45 GMT Organization: Sun Microsystems Inc. Message-ID: <3vo825$k9n@engnews2.Eng.Sun.COM> References: <fwv3ffkzm98.fsf@oculus.aifh.ed.ac.uk> Reply-To: clamage@Eng.Sun.COM NNTP-Posting-Host: taumet.eng.sun.com