C++ Logo

std-discussion

Advanced search

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

From: Matthew Woehlke <mwoehlke.floss_at_[hidden]>
Date: Thu, 6 Aug 2020 14:46:25 -0400
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

Received on 2020-08-06 13:49:50