Topic: Casting bool to char


Author: David Wood <david@wood2.demon.co.uk>
Date: 1999/05/22
Raw View
Can anyone tell me what the position is on casting a (plain, not
explicitly signed or unsigned) char to bool? Does the standard guarantee
that this will always work?

I write with reference to the answer to Stroustrup's exercise 4.7 in
Vandevoorde's excellent "C++ solutions" book that I was looking over for
revision purposes. Vandevoorde omits char from his graph, whilst his
explanatory notes indicate that a char is always treated as a signed or
unsigned char by the compiler, though the standard doesn't mandate which
is used (does the standard mandate that char must be one of signed char
and unsigned char?).

Certainly Visual C++ 6 treats plain chars as signed unless you use a
compiler switch to flick the default to unsigned - though that, of
course, gives no indication as to what the standard says (and I don't
have a copy of the standard document to hand).





David
--
David Wood
david@wood2.demon.co.uk
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: juergen@monocerus.demon.co.uk (Juergen Heinzl)
Date: 1999/05/22
Raw View
In article <j8u3udA4$vR3EAKZ@wood2.demon.co.uk>, David Wood wrote:
>Can anyone tell me what the position is on casting a (plain, not
>explicitly signed or unsigned) char to bool? Does the standard guarantee
>that this will always work?
>
>I write with reference to the answer to Stroustrup's exercise 4.7 in
>Vandevoorde's excellent "C++ solutions" book that I was looking over for
>revision purposes. Vandevoorde omits char from his graph, whilst his
>explanatory notes indicate that a char is always treated as a signed or
>unsigned char by the compiler, though the standard doesn't mandate which
>is used (does the standard mandate that char must be one of signed char
>and unsigned char?).

Yes, you cannot rely on char == signed char, but since anything not 0
casted to bool will result in true there is no problem.
[...]

Cheers,
Juergen

--
\ Real name     : J|rgen Heinzl                 \       no flames      /
 \ EMail Private : juergen@monocerus.demon.co.uk \ send money instead /
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/05/23
Raw View
David Wood <david@wood2.demon.co.uk> writes:

>Can anyone tell me what the position is on casting a (plain, not
>explicitly signed or unsigned) char to bool? Does the standard guarantee
>that this will always work?

Yes. Any arithmetic type can be converted to a bool (4.12), and
a plain char is an arithmetic type (3.9.1).

>... Vandevoorde omits char from his graph, whilst his
>explanatory notes indicate that a char is always treated as a signed or
>unsigned char by the compiler, though the standard doesn't mandate which
>is used (does the standard mandate that char must be one of signed char
>and unsigned char?).

It must be implemented the same way as one or the other, but it is
nonetheless a different type (3.9.1). Similarly, type int on many
systems happens to be implemented the same way as type short or
type long.  Yet all three are distict types.

--
Steve Clamage, stephen.clamage@sun.com
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1999/05/23
Raw View
David Wood <david@wood2.demon.co.uk> wrote:

: Can anyone tell me what the position is on casting a (plain, not
: explicitly signed or unsigned) char to bool? Does the standard guarantee
: that this will always work?

Yes.

: I write with reference to the answer to Stroustrup's exercise 4.7 in
: Vandevoorde's excellent "C++ solutions" book that I was looking over for
: revision purposes. Vandevoorde omits char from his graph, whilst his
: explanatory notes indicate that a char is always treated as a signed or
: unsigned char by the compiler, though the standard doesn't mandate which
: is used (does the standard mandate that char must be one of signed char
: and unsigned char?).

3.9.1

No.  Char, signed char, and unsigned char are three distinct types.

Yes.  The size and alignment of all three must be the same.  The
values which a char can take must be the same as one of the other
two types.

So, the compiler will treat values of a char the same as one of the
others.

John



[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]