C++ Logo

std-discussion

Advanced search

Re: What makes bool{2} ill-formed?

From: Andrew Tomazos <andrewtomazos_at_[hidden]>
Date: Fri, 7 Aug 2020 07:06:08 +1000
Hey! Poor old not-equals is like "what am I? chopped liver?"

    auto x = (2 != 0);

In fact many languages do not allow "conversion" of a number to boolean.
If you want a condition that is the number is not zero then you simply say
just that.

On Fri, Aug 7, 2020 at 4:46 AM Matthew Woehlke via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> On 06/08/2020 11.30, Florian Weimer via Std-Discussion wrote:
> > I don't think there is another meaningful way to get an int into a bool.
>
> auto x = static_cast<bool>(2);
> auto x = !!2;
> auto x = (2 ? true : false);
>
> I don't see the problem here; `bool{2}` is invalid (narrowing) for the
> same reason `char{65536}` is invalid. (Numerically speaking, the set of
> values which `bool` can represent is {0, 1}.)
>
> --
> Matthew
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>

Received on 2020-08-06 16:09:43