Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: c99 and the lack of warnings when int operations are applied to a bool |
| Date | 2012-03-27 11:07 -0700 |
| Organization | None to speak of |
| Message-ID | <lnhax9hq8a.fsf@nuthaus.mib.org> (permalink) |
| References | <jki5iu$jea$1@dont-email.me> <jkinde$or2$1@speranza.aioe.org> <jksrbo$p2a$1@dont-email.me> |
mathog <dmathog@gmail.com> writes:
> jacob navia wrote:
>> I think you have an important point here. Bool is a logical value, not
>> an arithmetic or floating point value. There is no sense in taking the
>> square root of a bool, nor there is any sense in dividing a bool by any
>> integer: the result is always zero unless the other number is 1.
>
> 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???
> If not, then they should, at least optionally, as for instance if one
> used gcc's -Wall.
The standard *never* forbids warnings for anything. It specifies
constructs for which an implementation must issue a diagnostic,
which may be either a warning or a fatal error, and it specifies
exactly one case where a translation unit must be rejected (#error),
but compilers can issue any additional warnings they like.
There are languages in which bool, or Boolean, is a completely
seperate type. In Ada, for example, it's defined as an enumeration
type with some special properties (type Boolean is (False, True);),
and Ada enumeration types in general can't be mixed with integer
types. For example, there's no predefined addition operator for
Boolean, or for any other enumeration type.
C *currently* defines _Bool as an unsigned integer type that can
hold the values 0 and 1. There are some special rules regarding
conversions from other scalar types to _Bool, but you can do the
same implicit conversions between _Bool and any other arithmetic
type as you can for char, int, and so forth.
Personally, I like the Ada approach better. But I'm not sure it
would have been practical to use the same approach in C. It would
have required creating a whole new class of type, with _Bool as its
only member. If it's a scalar type, you'd have to add special-case
restrictions to forbid certain implicit conversions; otherwise
"_Bool b = 0; b += 2;" would be perfectly legal (as it is now).
It's almost inevitable that some of those restrictions would prevent
programmers from doing some perfectly sensible things.
If you don't allow 0 and 1 to be assigned to a _Bool object,
requiring literal false and true instead, then you have an elementary
type for which 0 is not a valid value. Currently, this:
some_type obj = { 0 };
is valid for any object type; would it become invalid if some_type
is a structure one of whose members is a structure containing a
_Bool member? Or would you have to permit 0 and 1 to be boolean
constants, just as 0 is a pointer constant?
What would be the required type of the condition in an if statement?
Forbidding integer, floating-point, and pointer expressions would
break most existing C programs. Obviously we need to permit _Bool
conditions. Is that a special case? Do we permit an implicit
conversion from any scalar type to _Bool? Does that conversion
occur everywhere (as it does now), or only in an expression used
as a condition?
I'm not saying that it would have been impossible to add a boolean
type that behaves as suggested, but I think it would have required
a lot more work to define it consistently, and I rather doubt that
the C committee (which consists of volunteers, remember) could have
afforded the time to do it. They might have considered it if there
were existing practice, or if someone had submitted a proposal,
but as far as I know nobody did.
And I suggest that it's too late to fix it now without breaking
existing code. There is existing code that applies the ++ operator
to boolean objects, expecting them to be set to a true value (that
goes back before C99, when the "boolean" object was probably an int).
If I were designing a C-like language from scratch today, I'd
probably make bool a distinct type without implicit conversions, and
require "if (ptr != NULL)" rather than "if (ptr)", and "if (n != 0)"
rather than "if (n)". But we no longer have the luxury of doing that
-- and I'm sure a lot of people wouldn't like that approach anyway.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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