<p dir="ltr">On Dec 30, 2015 11:53 AM, "Aaron Ballman" <<a href="mailto:aaron.ballman@gmail.com">aaron.ballman@gmail.com</a>> wrote:<br>
><br>
> On Wed, Dec 30, 2015 at 12:30 PM, Richard Smith <<a href="mailto:richard@metafoo.co.uk">richard@metafoo.co.uk</a>> wrote:<br>
> > On Dec 30, 2015 6:34 AM, "Aaron Ballman" <<a href="mailto:aaron.ballman@gmail.com">aaron.ballman@gmail.com</a>> wrote:<br>
> >><br>
> >> aaron.ballman closed this revision.<br>
> >> aaron.ballman added a comment.<br>
> >><br>
> >> Thanks! I've commit in r256643.<br>
> >><br>
> >><br>
> >> ================<br>
> >> Comment at: test/SemaCXX/warn-literal-conversion.cpp:49-50<br>
> >> @@ +48,4 @@<br>
> >> +  // values.<br>
> >> +  bool b3 = 0.0f;<br>
> >> +  bool b4 = 0.0;<br>
> >> +}<br>
> >> ----------------<br>
> >> rsmith wrote:<br>
> >> > What about<br>
> >> ><br>
> >> >   bool b5 = 1.0;<br>
> >> >   bool b6 = 2.0;<br>
> >> ><br>
> >> > ? Arguably any `float` -> `bool` conversion changes the value (because<br>
> >> > `true` and `false` are not values of type `float`), so it wouldn't be<br>
> >> > completely unreasonable to warn even if the literal is `0.0`.<br>
> >> Except those conversions won't cause confusion to the user, so I'm not<br>
> >> certain what we gain by diagnosing. Given that some mental models expect<br>
> >> 0.99 to convert to 0, which converts to false (because bool is an integral<br>
> >> type, so it "must" do the usual integral truncation dance), it makes sense<br>
> >> to tell the user "no no no, that converts to true." I'm less convinced about<br>
> >> the utility of warning on things like `bool b = 1.99f' where it changes the<br>
> >> value from 1.99 to true. Perhaps this should be changed to only diagnose<br>
> >> when converting through an integer would result in a different value that<br>
> >> converting through the float?<br>
> ><br>
> > Let me ask a slightly different question: when would it ever be reasonable<br>
> > and intentional to initialize a bool from a floating-point *literal*? For<br>
> > integers, people use things like<br>
> ><br>
> >   int n = 1e6;<br>
> ><br>
> > which is why we don't complain if the literal is exactly representable in<br>
> > the integer type. I can't imagine any similar cases for bool.<br>
><br>
> I see what you're getting at now, and I agree that it wouldn't be<br>
> particularly reasonable. However, I think that there are two things to<br>
> warn about: initializing from a literal in general, and initializing<br>
> from a literal where the resulting Boolean value may be surprising.<br>
> While we're at it: is there ever a time where it's reasonable to<br>
> initialize a bool from a literal of type other than an integer or<br>
> bool? For instance:<br>
><br>
> bool b1 = L'1';<br>
> bool b2 = '1';<br>
> bool b3 = 1.0f;<br>
> bool b4 = nullptr; // -Wnull-conversion catches this<br>
><br>
> I think it makes sense for all of these to diagnose under<br>
> -Wliteral-conversion, but drop the "changes value" clause.<br>
><br>
> bool b5 = 0.01;<br>
> bool b6 = '0';<br>
> bool b7 = L'0';<br>
><br>
> I think it makes sense for these to diagnose under<br>
> -Wliteral-conversion, but with the changes value clause.</p>
<p dir="ltr">That makes sense to me. IIRC, we have seen initialization of a bool from a string literal "true" or -- worse -- "false" a few times.</p>
<p dir="ltr">> ~Aaron<br>
><br>
> ><br>
> >><br>
> >> <a href="http://reviews.llvm.org/D15814">http://reviews.llvm.org/D15814</a><br>
> >><br>
> >><br>
> >><br>
</p>