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>=
&nbsp; void operator()(const std::true&lt;handler_concept&gt;&amp; bt) ;<br=
>&nbsp; void operator()(const std::false&lt;handler_concept&gt;&amp; bf) ;<=
br>&nbsp; void operator()(const std::bool_intermediate&lt;handler_concept&g=
t;&amp; bi) ;<br>...<br>};<br><br>&nbsp; handler_concept hc; // class objec=
t that handles boolean states<br>&nbsp; std::boolean&lt;handler_concept&gt;=
 bln(hc); // creation of std::boolean object that will invoke method on han=
dler_concept object hc<br><br>&nbsp; SomeOperation s_op;&nbsp; // class obj=
ect with boolean method<br>&nbsp; bln =3D s_op.some_bool_function();&nbsp; =
// 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 &amp; Regards,<=
br>--James Smith<br><br><br>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
&nbsp;<br />
&nbsp;<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>&nbsp;</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?&nbsp; 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>&nbsp;</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>&nbsp; void operator()(const std::true&lt;=
handler_concept&gt;&amp; bt) ;<br>&nbsp; void operator()(const std::false&l=
t;handler_concept&gt;&amp; bf) ;<br>&nbsp; void operator()(const std::bool_=
intermediate&lt;<wbr>handler_concept&gt;&amp; bi) ;<br>...<br>};<br><br>&nb=
sp; handler_concept hc; // class object that handles boolean states<br>&nbs=
p; std::boolean&lt;handler_concept&gt; bln(hc); // creation of std::boolean=
 object that will invoke method on handler_concept object hc<br><br>&nbsp; =
SomeOperation s_op;&nbsp; // class object with boolean method<br>&nbsp; bln=
 =3D s_op.some_bool_function();&nbsp; // 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>&nbsp;</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?&nbsp; 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?&nbsp; What problem are you solving?<br><br><br><br>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
&nbsp;<br />
&nbsp;<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. &nbsp;... Also, std::bool_intermediate take into accoun=
t the superposition of the two boolean states.&nbsp;</blockquote><div><br><=
/div><div>This sounds exactly like std::optional&lt;bool&gt;. &nbsp;<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">&nbsp; void operator()(std::optional&lt;bool&gt; const &amp;res)</fon=
t><div><font color=3D"#500050">&nbsp; &nbsp; {</font></div><div>&nbsp; &nbs=
p; if(!res)</div><div>&nbsp; &nbsp; &nbsp; if_indeterminate();<br></div><di=
v>&nbsp; &nbsp; else if(*res)<br></div><div>&nbsp; &nbsp; &nbsp; if_true();=
</div><div>&nbsp; &nbsp; else</div><div>&nbsp; &nbsp; &nbsp; if_false();</d=
iv><div>&nbsp; &nbsp; }<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);">&nbsp;=
 virtual void if_true() =3D 0;</span><br style=3D"color: rgb(80, 0, 80);"><=
span style=3D"color: rgb(80, 0, 80);">&nbsp; virtual void if_false() =3D 0;=
</span><br style=3D"color: rgb(80, 0, 80);"><span style=3D"color: rgb(80, 0=
, 80);">&nbsp; 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>&nbsp; SomeOperation s_op;&nbsp; =
// class object with std::optional&lt;bool&gt; method<br>&nbsp;&nbsp;<span =
style=3D"color: rgb(80, 0, 80);">optional_bool_dispatch</span>_derived hc; =
// class derived from&nbsp;<span style=3D"color: rgb(80, 0, 80);">optional_=
bool_dispatch</span><br><div>&nbsp; hc(s_op.some_bool_function());</div></d=
iv></div></div></div><div><br></div>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; 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 />
&nbsp;<br />
&nbsp;<br />

------=_Part_132_20407235.1372095651354--

.