[cfe-dev] why is such a code allowed?

Daniel Dilts diltsman at gmail.com
Fri Feb 6 09:35:34 PST 2015


The paragraph in question (C++11) Is 4.7/4.  The second sentence covers the
particular case in question.

If the destination type is bool, see 4.12. If the source type is bool, the
value false is converted to zero and the value true is converted to one.

As for why, it can be useful to add a bool to an integer that is counting
the number of times a function passed.  Then you can just write myBool +=
FunctionToTest(); rather than some conditional block.

On Fri, Feb 6, 2015 at 8:51 AM, James Molloy <james at jamesmolloy.co.uk>
wrote:

> Hi Guy,
>
> C89 doesn't have the concept of "bool" - all comparisons return an int (or
> is it an unsigned int?) in the range [0,1].
>
> C99 and C++ build on C89 so even though they have the concept of "bool",
> the type of a comparison was not changed.
>
> Cheers,
>
> James
>
> [Even though this C-language question seems easy enough for me to answer,
> I am certain that a true language lawyer will pipe up and tell me I'm wrong
> ;) ]
>
> On Fri Feb 06 2015 at 4:37:11 PM Guy Maurel <guy.maurel at kde.org> wrote:
>
>> Hello!
>>
>> Let say:
>>     int a = 5;
>>     int b = 7;
>>     int i = 20;
>>     if ((a  == b) + i) {
>>         a = 15;
>>     }
>> My question is:
>> The expression (a == b) gives a bool result.
>> Why is it allowed to add a "bool" and an "int".
>>
>> Thanks
>> --
>> Guy Maurel
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150206/c4bbf06e/attachment.html>


More information about the cfe-dev mailing list