C++ Logo

std-discussion

Advanced search

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

From: Johannes Schaub <schaub.johannes_at_[hidden]>
Date: Thu, 6 Aug 2020 21:55:45 +0200
Am Do., 6. Aug. 2020 um 20:46 Uhr schrieb Matthew Woehlke via
Std-Discussion <std-discussion_at_[hidden]>:

> 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}.)
>
>
I made an issue report years ago about this, and the discussion
unfortunately ended up being about whether 'false' is a valid null pointer
constant, rather than more generally whether the set of values of bool
actually _is_ {0, 1}.
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_closed.html#1448 .

Received on 2020-08-06 15:02:47