<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	
	>
<channel>
	<title>
	Comments on: Passing Booleans to an Interface in an Expressive Way	</title>
	<atom:link href="https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/</link>
	<description>Jonathan Boccara&#039;s blog</description>
	<lastBuildDate>Tue, 12 Jun 2018 21:04:00 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.3</generator>
	<item>
		<title>
		By: Florian Wolters		</title>
		<link>https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-1060</link>

		<dc:creator><![CDATA[Florian Wolters]]></dc:creator>
		<pubDate>Tue, 12 Jun 2018 21:04:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1840#comment-1060</guid>

					<description><![CDATA[I recommend choosing the strong enum approach instead:

&lt;pre&gt;&lt;code class=&quot;c++&quot;&gt;
#include 

class Goblin final {
 public:
  enum class Wear : bool { NoHat = false, Hat = true };

  static bool wearAHat(const Wear obj) {
    return static_cast&#060;std::underlying_type::type&#062;(obj);
  }

  static Wear wearAHat(const bool is_wearing_hat) {
    return static_cast(is_wearing_hat);
  }

  explicit Goblin(const Wear wear) : wear_{wear} {
  }

  bool isWearingHat() const { return wearAHat(wear_); }

 private:
  Wear wear_;
};

std::ostream&#038; operator&#060;&#060;(std::ostream&#038; os, Goblin::Wear obj) {
  switch (obj) {
    case Goblin::Wear::NoHat:
      os &#060;&#060; &#034;NoHat&#034;;
      break;
    case Goblin::Wear::Hat:
      os &#060;&#060; &#034;Hat&#034;;
      break;
    default:
      os.setstate(std::ios::failbit);
  }

  return os;
}

int main() {
  // Strongly-typed initialization.
  const Goblin bill{Goblin::Wear::Hat};

  // Conversion to bool.
  std::cout &#060;&#060; &#034;Goblin::Wear::NoHat corresponds to: &#034;
            &#060;&#060; Goblin::wearAHat(Goblin::Wear::NoHat) &#060;&#060; &#039;n&#039;;
  std::cout &#060;&#060; &#034;Goblin::Wear::Hat corresponds to: &#034;
            &#060;&#060; Goblin::wearAHat(Goblin::Wear::Hat) &#060;&#060; &#039;n&#039;;
  std::cout &#060;&#060; &#034;is bill wearing a hat? &#034; &#060;&#060; std::boolalpha
            &#060;&#060; bill.isWearingHat() &#060;&#060; &#039;n&#039;;

  // Conversion from bool.
  const Goblin john{Goblin::wearAHat(false)};
  std::cout &#060;&#060; &#034;is john wearing a hat? &#034; &#060;&#060; std::boolalpha
            &#060;&#060; john.isWearingHat() &#060;&#060; &#039;n&#039;;
  std::cout &#060;&#060; &#034;false corresponds to: &#034; &#060;&#060; Goblin::wearAHat(false) &#060;&#060; &#039;n&#039;;
  std::cout &#060;&#060; &#034;true corresponds to: &#034; &#060;&#060; Goblin::wearAHat(true) &#060;&#060; &#039;n&#039;;
}
&lt;/code&gt;&lt;/pre&gt;

One can argue that is overkill, but I think it is almost always worth the effort. The caller has to invoke `Goblin{Goblin::wearAHat(true)}` instead of `Goblin{true}` and that is a &lt;b&gt;good thing&lt;/b&gt;, imo.

Off-topic: Disqus syntax highlighting does not work properly.]]></description>
			<content:encoded><![CDATA[<p>I recommend choosing the strong enum approach instead:</p>
<pre><code class="c++">
#include 

class Goblin final {
 public:
  enum class Wear : bool { NoHat = false, Hat = true };

  static bool wearAHat(const Wear obj) {
    return static_cast&lt;std::underlying_type::type&gt;(obj);
  }

  static Wear wearAHat(const bool is_wearing_hat) {
    return static_cast(is_wearing_hat);
  }

  explicit Goblin(const Wear wear) : wear_{wear} {
  }

  bool isWearingHat() const { return wearAHat(wear_); }

 private:
  Wear wear_;
};

std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, Goblin::Wear obj) {
  switch (obj) {
    case Goblin::Wear::NoHat:
      os &lt;&lt; &quot;NoHat&quot;;
      break;
    case Goblin::Wear::Hat:
      os &lt;&lt; &quot;Hat&quot;;
      break;
    default:
      os.setstate(std::ios::failbit);
  }

  return os;
}

int main() {
  // Strongly-typed initialization.
  const Goblin bill{Goblin::Wear::Hat};

  // Conversion to bool.
  std::cout &lt;&lt; &quot;Goblin::Wear::NoHat corresponds to: &quot;
            &lt;&lt; Goblin::wearAHat(Goblin::Wear::NoHat) &lt;&lt; &#039;n&#039;;
  std::cout &lt;&lt; &quot;Goblin::Wear::Hat corresponds to: &quot;
            &lt;&lt; Goblin::wearAHat(Goblin::Wear::Hat) &lt;&lt; &#039;n&#039;;
  std::cout &lt;&lt; &quot;is bill wearing a hat? &quot; &lt;&lt; std::boolalpha
            &lt;&lt; bill.isWearingHat() &lt;&lt; &#039;n&#039;;

  // Conversion from bool.
  const Goblin john{Goblin::wearAHat(false)};
  std::cout &lt;&lt; &quot;is john wearing a hat? &quot; &lt;&lt; std::boolalpha
            &lt;&lt; john.isWearingHat() &lt;&lt; &#039;n&#039;;
  std::cout &lt;&lt; &quot;false corresponds to: &quot; &lt;&lt; Goblin::wearAHat(false) &lt;&lt; &#039;n&#039;;
  std::cout &lt;&lt; &quot;true corresponds to: &quot; &lt;&lt; Goblin::wearAHat(true) &lt;&lt; &#039;n&#039;;
}
</code></pre>
<p>One can argue that is overkill, but I think it is almost always worth the effort. The caller has to invoke `Goblin{Goblin::wearAHat(true)}` instead of `Goblin{true}` and that is a <b>good thing</b>, imo.</p>
<p>Off-topic: Disqus syntax highlighting does not work properly.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-974</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Mon, 07 May 2018 01:36:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1840#comment-974</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-971&quot;&gt;CapSel&lt;/a&gt;.

Oh right yes, it seems to be a proposal http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0329r0.pdf. Thanks for sharing!]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-971">CapSel</a>.</p>
<p>Oh right yes, it seems to be a proposal <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0329r0.pdf" rel="nofollow ugc">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0329r0.pdf</a>. Thanks for sharing!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Wojciech Razik		</title>
		<link>https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-972</link>

		<dc:creator><![CDATA[Wojciech Razik]]></dc:creator>
		<pubDate>Sun, 06 May 2018 19:55:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1840#comment-972</guid>

					<description><![CDATA[Thanks for the great blogpost Jonathan! A while ago I read similar blog post from akrzemi: https://akrzemi1.wordpress.com/2017/02/16/toggles-in-functions/ , he wrote really nice library called tagged_bool. Take a look!]]></description>
			<content:encoded><![CDATA[<p>Thanks for the great blogpost Jonathan! A while ago I read similar blog post from akrzemi: <a href="https://akrzemi1.wordpress.com/2017/02/16/toggles-in-functions/" rel="nofollow ugc">https://akrzemi1.wordpress.com/2017/02/16/toggles-in-functions/</a> , he wrote really nice library called tagged_bool. Take a look!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: CapSel		</title>
		<link>https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-971</link>

		<dc:creator><![CDATA[CapSel]]></dc:creator>
		<pubDate>Sun, 06 May 2018 11:24:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1840#comment-971</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-970&quot;&gt;Jonathan Boccara&lt;/a&gt;.

I just found out that this feature is called &quot;Designated initializers&quot; and is not well supported on gcc - just clang.

example:
struct object {
  struct params {
    bool some_value1 = true;
    bool some_value2 = true;
  };
  object(const params&#038; p) {
    if (p.some_value1) {
      cout &#060;&#060; &#034;some_value1 is set&#034; &#060;&#060; endl;
    }
    if (p.some_value2) {
      cout &#060;&#060; &#034;some_value2 is set&#034; &#060;&#060; endl;
    }
  }
};

int main() {
  object foo({ .some_value2 = false });
}]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-970">Jonathan Boccara</a>.</p>
<p>I just found out that this feature is called &#8220;Designated initializers&#8221; and is not well supported on gcc &#8211; just clang.</p>
<p>example:<br />
struct object {<br />
  struct params {<br />
    bool some_value1 = true;<br />
    bool some_value2 = true;<br />
  };<br />
  object(const params&amp; p) {<br />
    if (p.some_value1) {<br />
      cout &lt;&lt; &quot;some_value1 is set&quot; &lt;&lt; endl;<br />
    }<br />
    if (p.some_value2) {<br />
      cout &lt;&lt; &quot;some_value2 is set&quot; &lt;&lt; endl;<br />
    }<br />
  }<br />
};</p>
<p>int main() {<br />
  object foo({ .some_value2 = false });<br />
}</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-970</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Sat, 05 May 2018 21:47:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1840#comment-970</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-966&quot;&gt;CapSel&lt;/a&gt;.

Thanks for your suggestion. Do you have an example of C++ code that uses that syntax and compiles?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-966">CapSel</a>.</p>
<p>Thanks for your suggestion. Do you have an example of C++ code that uses that syntax and compiles?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-969</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Sat, 05 May 2018 21:43:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1840#comment-969</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-967&quot;&gt;Colin Pitrat&lt;/a&gt;.

Interesting! Do you know where to read more about this syntax check by clang?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-967">Colin Pitrat</a>.</p>
<p>Interesting! Do you know where to read more about this syntax check by clang?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-968</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Sat, 05 May 2018 21:42:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1840#comment-968</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-961&quot;&gt;Gerald Squelart&lt;/a&gt;.

I think this is an interesting trade-off. On one hand you can&#039;t receive a bool coming from raw data (GoblinUserInput) (but you could argue this makes sense and you&#039;d need an indirection to transform raw data into business data). And on the other hand, like you&#039;re saying, you can&#039;t pass a bool that has another meaning. Thanks for sharing your opinion.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-961">Gerald Squelart</a>.</p>
<p>I think this is an interesting trade-off. On one hand you can&#8217;t receive a bool coming from raw data (GoblinUserInput) (but you could argue this makes sense and you&#8217;d need an indirection to transform raw data into business data). And on the other hand, like you&#8217;re saying, you can&#8217;t pass a bool that has another meaning. Thanks for sharing your opinion.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Colin Pitrat		</title>
		<link>https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-967</link>

		<dc:creator><![CDATA[Colin Pitrat]]></dc:creator>
		<pubDate>Sat, 05 May 2018 18:18:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1840#comment-967</guid>

					<description><![CDATA[The issue is that passing the wrong name (wearAHat) for another parameter (e.g  has_a_beard) is not detected.
I personnaly like the `function(/*varname=*/true)` syntax that can be checked by clang.]]></description>
			<content:encoded><![CDATA[<p>The issue is that passing the wrong name (wearAHat) for another parameter (e.g  has_a_beard) is not detected.<br />
I personnaly like the `function(/*varname=*/true)` syntax that can be checked by clang.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: CapSel		</title>
		<link>https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-966</link>

		<dc:creator><![CDATA[CapSel]]></dc:creator>
		<pubDate>Sat, 05 May 2018 11:41:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1840#comment-966</guid>

					<description><![CDATA[How about creating a public member struct called params and call Goblin constructor with it:
Goblin({ .wearsAHat = true, .wearsSomethingElse = true })

?]]></description>
			<content:encoded><![CDATA[<p>How about creating a public member struct called params and call Goblin constructor with it:<br />
Goblin({ .wearsAHat = true, .wearsSomethingElse = true })</p>
<p>?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Gerald Squelart		</title>
		<link>https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-965</link>

		<dc:creator><![CDATA[Gerald Squelart]]></dc:creator>
		<pubDate>Fri, 04 May 2018 08:46:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1840#comment-965</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-964&quot;&gt;Aurélien Gâteau&lt;/a&gt;.

Right, &#039;explicit&#039; is still desirable to avoid IsWearingAHat-&#062;Goblin implicit constructions if that&#039;s something you don&#039;t want. (There might be other scenarios where implicit construction would be wanted).



But I initially thought it was necessary so that the constructor woouldn&#039;t accept a pure bool alone, but I was incorrect about that part.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/05/04/passing-booleans-to-an-interface-in-an-expressive-way/#comment-964">Aurélien Gâteau</a>.</p>
<p>Right, &#8216;explicit&#8217; is still desirable to avoid IsWearingAHat-&gt;Goblin implicit constructions if that&#8217;s something you don&#8217;t want. (There might be other scenarios where implicit construction would be wanted).</p>
<p>But I initially thought it was necessary so that the constructor woouldn&#8217;t accept a pure bool alone, but I was incorrect about that part.</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
