Topic: C++ types std::boolean, std::true, std::false and std::bool_intermediate
Author: Jim Smith <jimsmithellis@gmail.com>
Date: Mon, 24 Jun 2013 01:37:47 -0700 (PDT)
Raw View
------=_Part_2571_22452694.1372063067988
Content-Type: text/plain; charset=ISO-8859-1
Hi,
I posted this once in more detail, but didn't see it listed.
I'm interested in an implementation of C++ types std::boolean, std::true,
std::false, and std::bool_intermediate for states that are not true or
false. These types would allow boolean results to be handled using OO
techniques instead of if/else statements. Also, std::bool_intermediate take
into account the superposition of the two boolean states.
An implementation of these types would allow the result of a boolean method
to be handled using multiple dispatch instead of traditional if/else
statements:
(multiple dispatch is explained in Scott Meyers "More Effective C++")
//usage example:
class handler_concept
{
public:
....
void operator()(const std::true<handler_concept>& bt) ;
void operator()(const std::false<handler_concept>& bf) ;
void operator()(const std::bool_intermediate<handler_concept>& bi) ;
....
};
handler_concept hc; // class object that handles boolean states
std::boolean<handler_concept> bln(hc); // creation of std::boolean object
that will invoke method on handler_concept object hc
SomeOperation s_op; // class object with boolean method
bln = s_op.some_bool_function(); // bln is assigned the boolean value
and invokes the method on handler_concept object hc
//end of example
I implemented these boolean types to try the purposed technique. It's more
elegant than if/else and allows each class to define its own meaning of
true or false etc., increasing encapsulation of an application's concepts.
Thanks & Regards,
--James Smith
--
---
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_2571_22452694.1372063067988
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Hi,<br><br>I posted this once in more detail, but didn't see it listed. <br=
><br>I'm interested in an implementation of C++ types std::boolean, std::tr=
ue, std::false, and std::bool_intermediate for states that are not true or =
false. These types would allow boolean results to be handled using OO techn=
iques instead of if/else statements. Also, std::bool_intermediate take into=
account the superposition of the two boolean states. <br><br>An implementa=
tion of these types would allow the result of a boolean method to be handle=
d using multiple dispatch instead of traditional if/else statements:<br><br=
>(multiple dispatch is explained in Scott Meyers "More Effective C++")<br><=
br>//usage example:<br><br>class handler_concept<br>{<br>public:<br>...<br>=
void operator()(const std::true<handler_concept>& bt) ;<br=
> void operator()(const std::false<handler_concept>& bf) ;<=
br> void operator()(const std::bool_intermediate<handler_concept&g=
t;& bi) ;<br>...<br>};<br><br> handler_concept hc; // class objec=
t that handles boolean states<br> std::boolean<handler_concept>=
bln(hc); // creation of std::boolean object that will invoke method on han=
dler_concept object hc<br><br> SomeOperation s_op; // class obj=
ect with boolean method<br> bln =3D s_op.some_bool_function(); =
// bln is assigned the boolean value and invokes the method on handler_conc=
ept object hc<br><br>//end of example<br><br>I implemented these boolean ty=
pes to try the purposed technique. It's more elegant than if/else and allow=
s each class to define its own meaning of true or false etc., increasing en=
capsulation of an application's concepts.<br><br><br>Thanks & Regards,<=
br>--James Smith<br><br><br>
<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 />
<br />
<br />
------=_Part_2571_22452694.1372063067988--
.
Author: Jonathan Wakely <cxx@kayari.org>
Date: Mon, 24 Jun 2013 03:25:21 -0700 (PDT)
Raw View
------=_Part_287_22858186.1372069521748
Content-Type: text/plain; charset=ISO-8859-1
On Monday, June 24, 2013 9:37:47 AM UTC+1, Jim Smith wrote:
>
> Hi,
>
> I posted this once in more detail, but didn't see it listed.
>
> I'm interested in an implementation of C++ types std::boolean, std::true,
> std::false, and std::bool_intermediate for states that are not true or
> false. These types would allow boolean results to be handled using OO
> techniques instead of if/else statements.
Why is that a good thing?
I know that type dispatching with an open set of types is much better
handled with virtual functions than with if/else statements, but boolean is
not an open set, it has true and it has false.
> Also, std::bool_intermediate take into account the superposition of the
> two boolean states.
>
>
This seems to be confusing two concepts: compile-time booleans (which we
have with std::true_type and std::false_type) and a tri-bool type. Why
would we want an intermediate state for booleans? Does it model this code?
if (some_condition)
{ }
else if (!some_condition)
{ }
else /* ??? */
{ }
If we can't do it with booleans, why should we be able to do it with a type
representing booleans?
> An implementation of these types would allow the result of a boolean
> method to be handled using multiple dispatch instead of traditional if/else
> statements:
>
> (multiple dispatch is explained in Scott Meyers "More Effective C++")
>
> //usage example:
>
> class handler_concept
> {
> public:
> ...
> void operator()(const std::true<handler_concept>& bt) ;
> void operator()(const std::false<handler_concept>& bf) ;
> void operator()(const std::bool_intermediate<handler_concept>& bi) ;
> ...
> };
>
> handler_concept hc; // class object that handles boolean states
> std::boolean<handler_concept> bln(hc); // creation of std::boolean
> object that will invoke method on handler_concept object hc
>
> SomeOperation s_op; // class object with boolean method
> bln = s_op.some_bool_function(); // bln is assigned the boolean value
> and invokes the method on handler_concept object hc
>
> //end of example
>
> I implemented these boolean types to try the purposed technique. It's more
> elegant than if/else and allows each class to define its own meaning of
> true or false etc., increasing encapsulation of an application's concepts.
>
How did you implement it when true and false are keywords?
What's so wrong with if/else that "OO techniques" are needed to replace
it? I don't consider your example elegant at all.
This might be useful in your own project, why should it be standardised?
What problem are you solving?
--
---
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_287_22858186.1372069521748
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
<br><br>On Monday, June 24, 2013 9:37:47 AM UTC+1, Jim Smith wrote:<blockqu=
ote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left=
: 1px #ccc solid;padding-left: 1ex;">Hi,<br><br>I posted this once in more =
detail, but didn't see it listed. <br><br>I'm interested in an implementati=
on of C++ types std::boolean, std::true, std::false, and std::bool_intermed=
iate for states that are not true or false. These types would allow boolean=
results to be handled using OO techniques instead of if/else statements.</=
blockquote><div><br>Why is that a good thing?<br><br>I know that type dispa=
tching with an open set of types is much better handled with virtual functi=
ons than with if/else statements, but boolean is not an open set, it has tr=
ue and it has false. <br> </div><blockquote class=3D"gmail_quote" styl=
e=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left:=
1ex;"> Also, std::bool_intermediate take into account the superposition of=
the two boolean states. <br><br></blockquote><div><br>This seems to be con=
fusing two concepts: compile-time booleans (which we have with std::true_ty=
pe and std::false_type) and a tri-bool type. Why would we want an intermedi=
ate state for booleans? Does it model this code?<br><br>if (some_cond=
ition)<br>{ }<br>else if (!some_condition)<br>{ }<br>else /* ??? */<br>{ }<=
br><br>If we can't do it with booleans, why should we be able to do it with=
a type representing booleans?<br><br> </div><blockquote class=3D"gmai=
l_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;=
padding-left: 1ex;">An implementation of these types would allow the result=
of a boolean method to be handled using multiple dispatch instead of tradi=
tional if/else statements:<br><br>(multiple dispatch is explained in Scott =
Meyers "More Effective C++")<br><br>//usage example:<br><br>class handler_c=
oncept<br>{<br>public:<br>...<br> void operator()(const std::true<=
handler_concept>& bt) ;<br> void operator()(const std::false&l=
t;handler_concept>& bf) ;<br> void operator()(const std::bool_=
intermediate<<wbr>handler_concept>& bi) ;<br>...<br>};<br><br>&nb=
sp; handler_concept hc; // class object that handles boolean states<br>&nbs=
p; std::boolean<handler_concept> bln(hc); // creation of std::boolean=
object that will invoke method on handler_concept object hc<br><br> =
SomeOperation s_op; // class object with boolean method<br> bln=
=3D s_op.some_bool_function(); // bln is assigned the boolean value =
and invokes the method on handler_concept object hc<br><br>//end of example=
<br><br>I implemented these boolean types to try the purposed technique. It=
's more elegant than if/else and allows each class to define its own meanin=
g of true or false etc., increasing encapsulation of an application's conce=
pts.<br></blockquote><div> </div><div>How did you implement it when tr=
ue and false are keywords?<br><br></div>What's so wrong with if/else that "=
OO techniques" are needed to replace it? I don't consider your exampl=
e elegant at all. <br><br>This might be useful in your own project, why sho=
uld it be standardised? What problem are you solving?<br><br><br><br>
<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 />
<br />
<br />
------=_Part_287_22858186.1372069521748--
.
Author: Greg Marr <gregmmarr@gmail.com>
Date: Mon, 24 Jun 2013 10:40:51 -0700 (PDT)
Raw View
------=_Part_132_20407235.1372095651354
Content-Type: text/plain; charset=ISO-8859-1
On Monday, June 24, 2013 4:37:47 AM UTC-4, Jim Smith wrote:
> std::bool_intermediate for states that are not true or false. ... Also,
> std::bool_intermediate take into account the superposition of the two
> boolean states.
This sounds exactly like std::optional<bool>.
If you really want OO behavior from a class here, I think this code would
be much clearer,
just derive from this dispatch class and implement the virtual functions.
class optional_bool_dispatch
{
public:
void operator()(std::optional<bool> const &res)
{
if(!res)
if_indeterminate();
else if(*res)
if_true();
else
if_false();
}
protected:
virtual void if_true() = 0;
virtual void if_false() = 0;
virtual void if_indeterminate() = 0;
};
SomeOperation s_op; // class object with std::optional<bool> method
optional_bool_dispatch_derived hc; // class derived from
optional_bool_dispatch
hc(s_op.some_bool_function());
--
---
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_132_20407235.1372095651354
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
On Monday, June 24, 2013 4:37:47 AM UTC-4, Jim Smith wrote:<br><blockquote =
class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1p=
x #ccc solid;padding-left: 1ex;">std::bool_intermediate for states that are=
not true or false. ... Also, std::bool_intermediate take into accoun=
t the superposition of the two boolean states. </blockquote><div><br><=
/div><div>This sounds exactly like std::optional<bool>. <br></d=
iv><div><br></div><div>If you really want OO behavior from a class here, I =
think this code would be much clearer,</div><div>just derive from this disp=
atch class and implement the virtual functions.<br></div><div><br></div><sp=
an style=3D"color: rgb(80, 0, 80);">class optional_bool_dispatch</span><br =
style=3D"color: rgb(80, 0, 80);"><span style=3D"color: rgb(80, 0, 80);">{</=
span><br style=3D"color: rgb(80, 0, 80);"><span style=3D"color: rgb(80, 0, =
80);">public:</span><br style=3D"color: rgb(80, 0, 80);"><font color=3D"#50=
0050"> void operator()(std::optional<bool> const &res)</fon=
t><div><font color=3D"#500050"> {</font></div><div> &nbs=
p; if(!res)</div><div> if_indeterminate();<br></div><di=
v> else if(*res)<br></div><div> if_true();=
</div><div> else</div><div> if_false();</d=
iv><div> }<br></div><div><br></div><div><div>protected:<br sty=
le=3D"color: rgb(80, 0, 80);"><span style=3D"color: rgb(80, 0, 80);"> =
virtual void if_true() =3D 0;</span><br style=3D"color: rgb(80, 0, 80);"><=
span style=3D"color: rgb(80, 0, 80);"> virtual void if_false() =3D 0;=
</span><br style=3D"color: rgb(80, 0, 80);"><span style=3D"color: rgb(80, 0=
, 80);"> virtual void if_indeterminate() =3D 0;</span><br style=3D"co=
lor: rgb(80, 0, 80);"><span style=3D"color: rgb(80, 0, 80);">};</span><div>=
<br style=3D"color: rgb(80, 0, 80);"><div> SomeOperation s_op; =
// class object with std::optional<bool> method<br> <span =
style=3D"color: rgb(80, 0, 80);">optional_bool_dispatch</span>_derived hc; =
// class derived from <span style=3D"color: rgb(80, 0, 80);">optional_=
bool_dispatch</span><br><div> hc(s_op.some_bool_function());</div></d=
iv></div></div></div><div><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 />
<br />
<br />
------=_Part_132_20407235.1372095651354--
.