Topic: sizeof bool vs. historical typedef char


Author: imp@village.org (Warner Losh)
Date: 1995/05/25
Raw View
In article <3q187n$377c@news-s01.ny.us.ibm.net>,
Jim McElroy <jimmc@ibm.net> wrote:
>As for the C++ programs out there that use "bool" as a variable name
>or whatever,

One data point.

In the OI library, a 250,000 C++ GUI class library, we had to change
~30 lines in 3 routines in two modules that had used bool as a
variable name when g++ became boolaware.  We had to change no lines of
code in the 50,000 line gui builder that used the library.

However, OI does define its own bool type:
 enum OI_bool {OI_no = 0, OI_yes = 1}
so that might have skewed the data.  Then again, OI prefixed almost
all of its external identifiers with OI_ or oi_ since namespace hadn't
been invented when it was designed (or at least it hadn't been widely
implemented 7 years ago).

Warner

--
Warner Losh  "VMS Forever"  home: imp@village.org
Cyberspace Development, Inc   work: imp@marketplace.com
Makers of TIA, The Internet Adapter.  http://marketplace.com/





Author: jimmc@ibm.net (Jim McElroy)
Date: 1995/05/25
Raw View
In <3prf9j$dv0@giga.bga.com>, jamshid@ses.com (Jamshid Afshar) writes:
>By choosing "bool" the committee has made matters no better for the
>C++ programmers who are already battling conflicting boolean type
>definitions, and as in your case they have made things a real mess for
>some people who were innocently and correctly using the identifier
>"bool".

No, they've made things much better.  Now, the battles with conflicting boolean
type definitions will quickly die out as new compilers come on stream.

As for the C++ programs out there that use "bool" as a variable name or whatever,
someone will have to spend the time to do a global name change through
the places where the name was used.  If they had chosen come other name (boole
or Bool or BOOL or <insert your favorite>) then some different people would have had
to edit *their* programs.

----
Jim McElroy
Bellevue, Washington






Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/05/26
Raw View
mrs@cygnus.com (Mike Stump) writes:

>Doug Maxey <dwm@falcon.mbsa.com> wrote:
>>In g++ sizeof bool == 4,
[...]
>>but the question is, is this implementation defined?
>
>No, it is unspecified as I recall.

Nope, according to the note in 5.3.3/1, it is implementation defined.

--
Fergus Henderson                       | I'll forgive even GNU emacs as
fjh@cs.mu.oz.au                        | long as gcc is available ;-)
http://www.cs.mu.oz.au/~fjh            |             - Linus Torvalds





Author: jamshid@ses.com (Jamshid Afshar)
Date: 1995/05/23
Raw View
In article <3pd6i1$fi4@giga.bga.com>, Doug Maxey <dwm@falcon.mbsa.com> wrote:
>Greetings,
>there is a debate on the ncurses-list on what size should be
>used for bool type.
>
>In many places in the ncurses code, bool is referenced, and historically
>it has been a typedef char.
>
>[trouble linking C++ and C code and writing portable headers because of this]

This is a perfect example of why using the already *very* commonly
used name "bool" was such a bad decision.  I personally don't think a
boolean type should even have been accepted, but if they had to invent
one why not use a less common name like "boole"?  That would even have
been kinda cute considering that's the real last name of the person
whom boolean algebra is named after.

By choosing "bool" the committee has made matters no better for the
C++ programmers who are already battling conflicting boolean type
definitions, and as in your case they have made things a real mess for
some people who were innocently and correctly using the identifier
"bool".

Jamshid Afshar
jamshid@ses.com





Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/05/24
Raw View
dwm@falcon.mbsa.com (Doug Maxey) writes:

>In g++ sizeof bool == 4, but the question is, is this implementation
>defined?

Yes.  See 5.3.3/1.

>9 Types  bool,  char, wchar_t, and the signed and unsigned integer types
>  are collectively called integral types.
>"
>I take the above to mean that char should also be `integral type' from
>para 9 above.  Does this need clarification, or changing?

No, `char' is supposed to be an integral type.

--
Fergus Henderson                       | I'll forgive even GNU emacs as
fjh@cs.mu.oz.au                        | long as gcc is available ;-)
http://www.cs.mu.oz.au/~fjh            |             - Linus Torvalds





Author: dwm@falcon.mbsa.com (Doug Maxey)
Date: 1995/05/17
Raw View
Greetings,
there is a debate on the ncurses-list on what size should be
used for bool type.

In many places in the ncurses code, bool is referenced, and historically
it has been a typedef char.

When compiling under 'c', sizeof bool == 1.

When linking or compiling under g++, the sizes of the entities will
be different, with no compiler warnings or errors,
resulting in tremendous amounts of grief (to myself anyway).

In g++ sizeof bool == 4, but the question is, is this implementation
defined?

I want to persuade the ncurses developers to
1) use another name and stay with the char typedef, or
2) stick with the size of bool under c++(g++).

The draft standard is a little vague around the sizing, in
[basic.fundamental]
"8 Values  of  type bool are either true or false.9) There are no signed,
  unsigned, short, or long bool types or values.   As  described  below,
  bool values behave as integral types.  Values of type bool participate
  in integral promotions (_conv.prom_, _expr.type.conv_).  Although val
  ues  of  type bool generally behave as signed integers, for example by
  promoting (_conv.prom_) to int instead of unsigned int, a  bool  value
  can successfully be stored in a bit-field of any (nonzero) size.

9 Types  bool,  char, wchar_t, and the signed and unsigned integer types
  are collectively called integral types.10) A synonym for integral type
  is  integer  type.  The representations of integral types shall define
  values by use of  a pure binary numeration system.

"
I take the above to mean that char should also be `integral type' from
para 9 above.  Does this need clarification, or changing?

..doug

* Doug Maxey +1 512 990-0689                   Doug.Maxey@mbsa.com *
* P.O. Box 1537                           Austin, Texas 78720-1537 *
* Net Answers  *Network and Application Solutions*  BSDI-AIX-HP-PC *






Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/05/17
Raw View
In article fi4@giga.bga.com, dwm@falcon.mbsa.com (Doug Maxey) writes:
>Greetings,
>there is a debate on the ncurses-list on what size should be
>used for bool type.
>
>In many places in the ncurses code, bool is referenced, and historically
>it has been a typedef char.
>
>When compiling under 'c', sizeof bool == 1.
>
>When linking or compiling under g++, the sizes of the entities will
>be different, with no compiler warnings or errors,
>resulting in tremendous amounts of grief (to myself anyway).
>
>In g++ sizeof bool == 4, but the question is, is this implementation
>defined?

If you mean the new C++ built-in type whose name is 'bool', yes, the size
is implementation-defined. One, two, and four bytes would all be reasonable
choices for bool. (An implementation could choose a size for bool that was
different from all other numeric types, but that seems unlikely in practice.)


>I want to persuade the ncurses developers to
>1) use another name and stay with the char typedef, or
>2) stick with the size of bool under c++(g++).

If you need to create data structures which will be treated the same by
C++ and C compilers, you cannot portably use the C++ 'bool' type, since
'bool' is not a C type. You would have to use conditional compilation in
C code to pick a built-in type with the same size (f there is one). This
strikes me as a bad idea.

In the data structures you should use a type name that all compilers that
you intend to use on the same platform will treat the same. Your best
bet will be to use a typedef whose name is not 'bool' (because it is a
reserved word in C++), but perhaps 'Bool' or 'boolean'. Make the typedef
char or short or int -- whatever type is convenient and consistent across
compilers. You can then use exactly the same header files for C and C++.

In the internal C++ code you can use type bool, since conversions to and
from all the numeric types are implicit. Be sure to use the common typedef
(Bool, boolean, whatever) for all external uses.

---
Steve Clamage, stephen.clamage@eng.sun.com







Author: mrs@cygnus.com (Mike Stump)
Date: 1995/05/17
Raw View
In article <3pd6i1$fi4@giga.bga.com>, Doug Maxey <dwm@falcon.mbsa.com> wrote:
>Greetings,
>there is a debate on the ncurses-list on what size should be
>used for bool type.
>
>In many places in the ncurses code, bool is referenced, and historically
>it has been a typedef char.
>
>When compiling under 'c', sizeof bool == 1.
>
>When linking or compiling under g++, the sizes of the entities will
>be different, with no compiler warnings or errors,
>resulting in tremendous amounts of grief (to myself anyway).

You cannot hope to use `bool' in C++ and char in C to mean the exact
same type.  This code is non-portable, and will break.  If you want to
do it, there are ways to write portable code.

Autoconf the size of bool in g++, and then use what is necessary in C
to `make it work.'

Another way, is to pick a portable type, like char, or int, or short,
and use it.

>In g++ sizeof bool == 4,

This is sometimes true, but not always.  If you assume it is always 4
on all machines, you'll be bit.  Just assume it is sizeof(bool) in
size, and you'll never be bit.

>but the question is, is this implementation defined?

No, it is unspecified as I recall.

>I want to persuade the ncurses developers to
>1) use another name and stay with the char typedef, or
>2) stick with the size of bool under c++(g++).
>
>The draft standard is a little vague around the sizing, in

This is on purpose.