[cfe-dev] why is such a code allowed?
Matthew Plant
maplant2 at illinois.edu
Fri Feb 6 08:33:54 PST 2015
From a C perspective this makes perfect sense as there is no "bool" types, if statements branch based on a value not being zero rather than not being logically true.
From a C++ perspective, where the typing is more strict, I have copied this bit from the Microsoft C++ site which was the first thing I could find on the subject and I assume is standard conforming:
The bool type participates in integral promotions. An r-value of type bool can be converted to an r-value of type int, with false becoming zero and true becoming one. As a distinct type, bool participates in overload resolution.
> On Feb 6, 2015, at 10:19 AM, 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20150206/a96672be/attachment.html>
More information about the cfe-dev
mailing list