Topic: Interest in removing implicit conversions to bool for
Author: Xeo <hivemaster@hotmail.de>
Date: Tue, 27 Aug 2013 05:05:32 -0700 (PDT)
Raw View
------=_Part_2499_23709936.1377605132302
Content-Type: text/plain; charset=ISO-8859-1
Conversions to bool are generally a good thing: they're like a uniform
interface for checking the "validity" of an object, if the "validity" is
unambiguous - like for pointers. However, as could be see with the
invention of the "safe-bool idiom", *implicit* conversions to bool can
cause many problems and aren't very favorable. C++11 added explicit
conversion operators and so-called *contextual conversions to bool*, which
is a definite improvement - but the built-in types still suffer from
implicit conversions.
On a slight tangent: Something that has formed in the back of my head in
the last few days was an unary postfix `operator?`, which explicitly asks
for a contextual conversion to bool. It's basically the conditional
operator without the branches.
The idea originally came up when somebody wanted to provide a function
which checks the validity of his object by inspecting the underlying
`std::optional`-like object which provides an `explicit operator bool`, but
not an `engaged()` or `initialized()` or similar function.
That could be seen as an interface-fail, but one has to live with it.
class foo{
std::optional<T> _impl;
// ...
bool test() const{ return _impl; }
};
This obviously won't compile, as the conversion to bool would be considered
*implicit* here. We then discussed several "workarounds":
return bool(_impl); // very subjective feeling: meeeeeeeh
return static_cast<bool>(_impl); // unwieldy
return _impl ? true : false; // feels redundant
return _impl && true; // feels redundant and obscure
return !!_impl; // double-banging in C++? oh come on
In comes `operator?`
return _impl?; // concise and (imo) clear
So, returning from that tangent, said `operator?` would then also be used
with the fundamental types, providing a clear migration path for code that
relies on the implicit conversion to bool.
There's no proposal at this point, as I just want to collect general
interest and feedback.
Regards
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.
------=_Part_2499_23709936.1377605132302
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Conversions to bool are generally a good thing: they're li=
ke a uniform interface for checking the "validity" of an object, if the "va=
lidity" is unambiguous - like for pointers. However, as could be see with t=
he invention of the "safe-bool idiom", *implicit* conversions to bool can c=
ause many problems and aren't very favorable. C++11 added explicit conversi=
on operators and so-called *contextual conversions to bool*, which is a def=
inite improvement - but the built-in types still suffer from implicit conve=
rsions.<br><br>On a slight tangent: Something that has formed in the back o=
f my head in the last few days was an unary postfix `operator?`, which expl=
icitly asks for a contextual conversion to bool. It's basically the conditi=
onal operator without the branches.<br>The idea originally came up when som=
ebody wanted to provide a function which checks the validity of his object =
by inspecting the underlying `std::optional`-like object which provides an =
`explicit operator bool`, but not an `engaged()` or `initialized()` or simi=
lar function.<br>That could be seen as an interface-fail, but one has to li=
ve with it.<br><br><div class=3D"prettyprint" style=3D"background-color: rg=
b(250, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; bo=
rder-width: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div c=
lass=3D"subprettyprint"><span style=3D"color: #008;" class=3D"styled-by-pre=
ttify">class</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"> foo</span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"><br> st=
d</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify">optional</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify"><</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify">T</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">></span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"> _impl</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br></span><span style=3D"color: #800;" class=3D"st=
yled-by-prettify">// ...</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"><br> </span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">bool</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> test</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">()</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </s=
pan><span style=3D"color: #008;" class=3D"styled-by-prettify">const</span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #008;" class=3D"styled-by-prettify">return</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"> _impl</span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">}</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
};</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></sp=
an></div></code></div><br>This obviously won't compile, as the conversion t=
o bool would be considered *implicit* here. We then discussed several "work=
arounds":<br><br><div class=3D"prettyprint" style=3D"background-color: rgb(=
250, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; bord=
er-width: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div cla=
ss=3D"subprettyprint"><span style=3D"color: #008;" class=3D"styled-by-prett=
ify">return</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">bool</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify">_impl</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">);</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #800;"=
class=3D"styled-by-prettify">// very subjective feeling: meeeeeeeh</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">return</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">static_cast</span><span style=3D"colo=
r: #080;" class=3D"styled-by-prettify"><bool></span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify">_impl</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">);</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> </span><span style=3D"color: #800;" class=3D"styled-by-=
prettify">// unwieldy</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"><br></span><span style=3D"color: #008;" class=3D"styled-by-prett=
ify">return</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> _impl </span><span style=3D"color: #660;" class=3D"styled-by-prettify">?<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><sp=
an style=3D"color: #008;" class=3D"styled-by-prettify">true</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">:</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">false</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> </span><span style=3D"color: #800;" class=3D"styled-by-prettify"=
>// feels redundant</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"><br></span><span style=3D"color: #008;" class=3D"styled-by-prettif=
y">return</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
_impl </span><span style=3D"color: #660;" class=3D"styled-by-prettify">&=
;&</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </s=
pan><span style=3D"color: #008;" class=3D"styled-by-prettify">true</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #800;" class=3D"styled-by-prettify">// feels redundant and obscure</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span =
style=3D"color: #008;" class=3D"styled-by-prettify">return</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">!!</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify">_impl</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> </span><span style=3D"color: #800;" class=3D"styled-by-pr=
ettify">// double-banging in C++? oh come on</span></div></code></div><br>I=
n comes `operator?`<br><br><div class=3D"prettyprint" style=3D"background-c=
olor: rgb(250, 250, 250); border-color: rgb(187, 187, 187); border-style: s=
olid; border-width: 1px; word-wrap: break-word;"><code class=3D"prettyprint=
"><div class=3D"subprettyprint"><span style=3D"color: #008;" class=3D"style=
d-by-prettify">return</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"> _impl</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">?;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
</span><span style=3D"color: #800;" class=3D"styled-by-prettify">// concise=
and (imo) clear</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"><br></span></div></code></div><br>So, returning from that tangent, sa=
id `operator?` would then also be used with the fundamental types, providin=
g a clear migration path for code that relies on the implicit conversion to=
bool.<br><br>There's no proposal at this point, as I just want to collect =
general interest and feedback.<br><br>Regards<br></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
------=_Part_2499_23709936.1377605132302--
.