Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #19084

Re: c99 and the lack of warnings when int operations are applied to a bool

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: c99 and the lack of warnings when int operations are applied to a bool
Date 2012-03-27 14:16 -0400
Organization A noiseless patient Spider
Message-ID <4F7203FC.2080305@verizon.net> (permalink)
References <jki5iu$jea$1@dont-email.me> <jkinde$or2$1@speranza.aioe.org> <jksrbo$p2a$1@dont-email.me>

Show all headers | View raw


On 03/27/2012 12:53 PM, mathog wrote:
...
> My point exactly, even if a bool in some implementation is stored more 
> or less the same way as an integer type, none of the integer operations 
> should be applicable to it.  The problem is clearer if one thinks of the 
> values of the bool as "True" and "False", as opposed to 0 or 1.  None of 
> these have any meaning:
> 
>    True++
>    True+10
>    True/True
>    True+False
>    etc.
> 
> Code that performed those operations is broken.  Yet all of those are 
> accepted without a hiccup at present.  This is different from the use of 
> an int as a logical value, where, since in some contexts the variable 
> might be used as an integer, and others as a logical value, all of the 
> above are OK.
> 
> Consider even a simple assignment:
> 
>    bool b;
>    int  i=25;
>    b=i;
> 
> is actually:
> 
>    b= (i ? True : False);
> 
> and is not actually a direct assignment of the value of i to b.  In 
> order to avoid difficult to find programming mistakes the explicit cast 
> should be required:
> 
>    b = (bool) i;
> 
> and also going the other way
> 
>    i = (int) b;
> 
> This would let the compiler do its job of finding suspect code.  If the 
> standard doesn't require it would still be a very useful compiler switch 
> as something like:
> 
>    cc --req_cast_bool
> 
> Does the standard say that these situations cannot generate a warning???

The standard never prohibits warnings, it only mandates diagnostic
messages. A fully conforming compiler is free to warn about anything
it's makers want it to warn about. It can warn about the fact that you
used profanity in your program - it could also warn about the fact that
you did NOT use profanity in your program.

No diagnostic is mandated for any of the cases you describe, because
_Bool values are implicitly promoted to 'int', for which all of those
operations are permitted and reasonable. Whether the implicit promotion
is reasonable is another question.

>   If not, then they should, at least optionally, as for instance if one 
> used gcc's -Wall.

You can't ask gcc to warn if a _Bool value is used in an addition
expression, because that never happens - the _Bool value is promoted to
'int' before it is added.

You can ask the makers of gcc to implement a warning for all implicit
conversions of _Bool to other types. No such conversion occurs in most
contexts where use of a boolean value actually makes sense: sizeof,
_Alignof, &&, ||, !, the left operand of ?:, simple assignment when the
destination is of _Bool type, if(), while(), the second expression of
for(), and the return statement of a function declared as returning
_Bool. The main exceptions are ==, !=, and switch(), which all perform
the integer promotions on _Bool values.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

c99 and the lack of warnings when int operations are applied to a bool mathog <dmathog@gmail.com> - 2012-03-23 08:40 -0700
  Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-03-23 12:07 -0400
  Re: c99 and the lack of warnings when int operations are applied to a bool jacob navia <jacob@spamsink.net> - 2012-03-23 21:45 +0100
    Re: c99 and the lack of warnings when int operations are applied to a bool Robert Wessel <robertwessel2@yahoo.com> - 2012-03-23 17:17 -0500
      Re: c99 and the lack of warnings when int operations are applied to a bool jacob navia <jacob@spamsink.net> - 2012-03-23 23:29 +0100
        Re: c99 and the lack of warnings when int operations are applied   to a bool Ian Collins <ian-news@hotmail.com> - 2012-03-24 11:48 +1300
          Re: c99 and the lack of warnings when int operations are applied   to a bool jacob navia <jacob@spamsink.net> - 2012-03-24 23:47 +0100
            Re: c99 and the lack of warnings when int operations are applied   to a bool Willem <willem@toad.stack.nl> - 2012-03-25 09:05 +0000
              Re: c99 and the lack of warnings when int operations are applied   to a bool "BartC" <bc@freeuk.com> - 2012-03-25 11:30 +0100
                Re: c99 and the lack of warnings when int operations are applied   to a bool Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-25 21:05 +0100
                Re: c99 and the lack of warnings when int operations are applied   to a bool Lowell Gilbert <lgusenet@be-well.ilk.org> - 2012-03-25 16:15 -0400
                Re: c99 and the lack of warnings when int operations are applied   to a bool Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-25 22:14 +0100
                Re: c99 and the lack of warnings when int operations are applied   to a bool James Kuyper <jameskuyper@verizon.net> - 2012-03-25 23:46 -0400
                Re: c99 and the lack of warnings when int operations are applied to a bool Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-26 12:05 +0100
                Re: c99 and the lack of warnings when int operations are applied to a bool Lowell Gilbert <lgusenet@be-well.ilk.org> - 2012-03-26 15:22 -0400
              Re: c99 and the lack of warnings when int operations are applied to a bool Gareth Owen <gwowen@gmail.com> - 2012-03-25 11:44 +0100
                Re: c99 and the lack of warnings when int operations are applied to a bool Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-25 13:06 +0100
            Re: c99 and the lack of warnings when int operations are applied to a bool Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-27 11:12 -0700
        Re: c99 and the lack of warnings when int operations are applied to a bool Robert Wessel <robertwessel2@yahoo.com> - 2012-03-24 01:15 -0500
          Re: c99 and the lack of warnings when int operations are applied to a bool jacob navia <jacob@spamsink.net> - 2012-03-24 23:29 +0100
    Re: c99 and the lack of warnings when int operations are applied to a bool mathog <dmathog@gmail.com> - 2012-03-27 09:53 -0700
      Re: c99 and the lack of warnings when int operations are applied to a bool Keith Thompson <kst-u@mib.org> - 2012-03-27 11:07 -0700
      Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-03-27 14:16 -0400
      Re: c99 and the lack of warnings when int operations are applied to a bool Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-26 07:57 -0400
        Re: c99 and the lack of warnings when int operations are applied to a bool jacob navia <jacob@spamsink.net> - 2012-04-26 14:08 +0200
        Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-04-26 09:09 -0400
          Re: c99 and the lack of warnings when int operations are applied to a bool Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-26 21:29 -0400
            Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-04-26 21:58 -0400
              Re: c99 and the lack of warnings when int operations are applied to a bool Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-04-26 22:32 -0400
                Re: c99 and the lack of warnings when int operations are applied to a bool James Kuyper <jameskuyper@verizon.net> - 2012-04-27 06:31 -0400
                Re: c99 and the lack of warnings when int operations are applied to a bool ImpalerCore <jadill33@gmail.com> - 2012-04-27 08:31 -0700

csiph-web