C++ Logo

STD-DISCUSSION

Advanced search

Subject: Re: What makes bool{2} ill-formed?
From: Matthew Woehlke (mwoehlke.floss_at_[hidden])
Date: 2020-08-06 13:46:25


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 list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups