<?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: Why Optional References Didn&#8217;t Make It In C++17	</title>
	<atom:link href="https://www.fluentcpp.com/2018/10/05/pros-cons-optional-references/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fluentcpp.com/2018/10/05/pros-cons-optional-references/</link>
	<description>Jonathan Boccara&#039;s blog</description>
	<lastBuildDate>Tue, 25 Dec 2018 04:35:26 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.4</generator>
	<item>
		<title>
		By: Flamefire		</title>
		<link>https://www.fluentcpp.com/2018/10/05/pros-cons-optional-references/#comment-1392</link>

		<dc:creator><![CDATA[Flamefire]]></dc:creator>
		<pubDate>Mon, 15 Oct 2018 11:34:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1853#comment-1392</guid>

					<description><![CDATA[IMO optional should be rebinding. It is simply consistent but I want to start with the most important argument (to me): The use case. optional has a clear semantic meaning that e.g. T* does not have: You may get a reference to an object. You are allowed to change that object, but you do not own it. A pointer is missing the last bit of information.

Now on consistency:

For optional (&quot;opt&quot;) and a value T (&quot;val&quot;) the following holds: 



- opt = val; -&#062; opt == val
- opt.get() = val -&#062; opt == val


So for optional this should also hold and it does for rebinding. Difference is that case 1 reassigns the reference and case 2 changes the object, but it is explicit in doing so.



Of course &quot;foo = val&quot; is ambiguous in the context where &quot;foo&quot; is of generic type (and possibly a reference).


But imagine &quot;T&quot; being a class and you want to call a method on it. You&#039;d have to do: opt-&#062;foobar().


So &quot;optional&quot; is ALREADY behaving like a pointer --&#062; Rebinding!


Note: I would also agree to make it explicit. Hence optional::operator=(U) is disallowed except when U is optional, null_opt, or reference_wrapper.


But I feel 1 case is forgotten in the discussion: opt1 = opt2. The expected behaviour for this is a change of the value of the optional -&#062; Rebinding for references.


TLDR: An optional already feels like a pointer with defined ownership (namely: none). Hence rebinding.]]></description>
			<content:encoded><![CDATA[<p>IMO optional should be rebinding. It is simply consistent but I want to start with the most important argument (to me): The use case. optional has a clear semantic meaning that e.g. T* does not have: You may get a reference to an object. You are allowed to change that object, but you do not own it. A pointer is missing the last bit of information.</p>
<p>Now on consistency:</p>
<p>For optional (&#8220;opt&#8221;) and a value T (&#8220;val&#8221;) the following holds: </p>
<p>&#8211; opt = val; -&gt; opt == val<br />
&#8211; opt.get() = val -&gt; opt == val</p>
<p>So for optional this should also hold and it does for rebinding. Difference is that case 1 reassigns the reference and case 2 changes the object, but it is explicit in doing so.</p>
<p>Of course &#8220;foo = val&#8221; is ambiguous in the context where &#8220;foo&#8221; is of generic type (and possibly a reference).</p>
<p>But imagine &#8220;T&#8221; being a class and you want to call a method on it. You&#8217;d have to do: opt-&gt;foobar().</p>
<p>So &#8220;optional&#8221; is ALREADY behaving like a pointer &#8211;&gt; Rebinding!</p>
<p>Note: I would also agree to make it explicit. Hence optional::operator=(U) is disallowed except when U is optional, null_opt, or reference_wrapper.</p>
<p>But I feel 1 case is forgotten in the discussion: opt1 = opt2. The expected behaviour for this is a change of the value of the optional -&gt; Rebinding for references.</p>
<p>TLDR: An optional already feels like a pointer with defined ownership (namely: none). Hence rebinding.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Gael Guennebaud		</title>
		<link>https://www.fluentcpp.com/2018/10/05/pros-cons-optional-references/#comment-1366</link>

		<dc:creator><![CDATA[Gael Guennebaud]]></dc:creator>
		<pubDate>Fri, 05 Oct 2018 17:06:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1853#comment-1366</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/10/05/pros-cons-optional-references/#comment-1364&quot;&gt;sibecker&lt;/a&gt;.

Similar thoughts here.  I would expect optional to be consistent with the following little wrapper W that looks like a int&#038;:

struct W {
  W(int&#038; x) : ref(x) {}
  W&#038; operator=(int x) { ref = x; return *this; }
  W&#038; operator=(W x) { ref = x.ref; return *this; }
  int&#038; ref;
};

in this case we already have:

int i1 = 1;
int i2 = 2;
optional o1 = i1;
optional o2 = i2;
o2 = o1; // set i2 to 1
o2 = nullptr;
o2 = o1; // rebind o2 to i1
int i3 = 3;
o2 = i3; // set i1 to 3

so the behavior of optional::operator= is already dependent on emptiness, and thus I don&#039;t see how a rebinding behavior for optional would bring any sort of consistency between optional ref and optionals in general.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/10/05/pros-cons-optional-references/#comment-1364">sibecker</a>.</p>
<p>Similar thoughts here.  I would expect optional to be consistent with the following little wrapper W that looks like a int&amp;:</p>
<p>struct W {<br />
  W(int&amp; x) : ref(x) {}<br />
  W&amp; operator=(int x) { ref = x; return *this; }<br />
  W&amp; operator=(W x) { ref = x.ref; return *this; }<br />
  int&amp; ref;<br />
};</p>
<p>in this case we already have:</p>
<p>int i1 = 1;<br />
int i2 = 2;<br />
optional o1 = i1;<br />
optional o2 = i2;<br />
o2 = o1; // set i2 to 1<br />
o2 = nullptr;<br />
o2 = o1; // rebind o2 to i1<br />
int i3 = 3;<br />
o2 = i3; // set i1 to 3</p>
<p>so the behavior of optional::operator= is already dependent on emptiness, and thus I don&#8217;t see how a rebinding behavior for optional would bring any sort of consistency between optional ref and optionals in general.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Gwen Roelants		</title>
		<link>https://www.fluentcpp.com/2018/10/05/pros-cons-optional-references/#comment-1365</link>

		<dc:creator><![CDATA[Gwen Roelants]]></dc:creator>
		<pubDate>Fri, 05 Oct 2018 13:04:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1853#comment-1365</guid>

					<description><![CDATA[Maybe it&#039;s also useful to ask the question of what advantage an optional would bring?
It seems pretty much identical to a regular T* pointer, except that a pointer already has clear and known notation for both rebinding and assignment.]]></description>
			<content:encoded><![CDATA[<p>Maybe it&#8217;s also useful to ask the question of what advantage an optional would bring?<br />
It seems pretty much identical to a regular T* pointer, except that a pointer already has clear and known notation for both rebinding and assignment.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: sibecker		</title>
		<link>https://www.fluentcpp.com/2018/10/05/pros-cons-optional-references/#comment-1364</link>

		<dc:creator><![CDATA[sibecker]]></dc:creator>
		<pubDate>Fri, 05 Oct 2018 11:28:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1853#comment-1364</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2018/10/05/pros-cons-optional-references/#comment-1362&quot;&gt;Timo Oster&lt;/a&gt;.

My thoughts exactly.  Based on the Single Resposibilty Princple, the job of optional is to add one more value to the range of types available, not to add new features (such as assigning to references).  The job of making references rebindable is done by reference_wrapper.  You can always combine the two: optional&#060;reference_wrapper&#062; and you have got rebinding behaviour with the null state:

using namespace std;
int i = 42;
int j = 27;
int k = -3;
optional&#060;reference_wrapper&#062; o1 = ref(i);
optional&#060;reference_wrapper&#062; o2 = ref(k);

o1 = ref(j); // o1 now references j.  i, j and j themselves are unchanged
o1 = o2; // o1 now references k.  i, j and k themselves are unchanged
o1-&#062;get() = 84; // updates k
o1 = nullopt; // o1 now references nothing at all.
o1-&#062;get() = -67; // Undefined behaviour - dereferencing a null optional

This behaves very much like a pointer without the arithmetic, albeit with different syntax, so we don&#039;t need optional to do that for us.

So if we already can have rebinding behaviour, we should make it assign to the underlying, just as references themselves do.  However, we have to ask ourselves what do we do if either the source or destination is null/unset?   The solution is to disallow assignment except when the source is nullopt_t and force the caller to dereference of the optional (to a T&#038;) to get assignment:

optional o3 = i;
optional o4 = k;

o3 = j; // doesn&#039;t compile
o3 = o4; // also doesn&#039;t compile
*o3 = j; // updates i so that i == j
o3 = nullopt; // this _is_ allowed and resets o3
*o3 = k; // Undefined behaviour - dereferencing a null optional]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2018/10/05/pros-cons-optional-references/#comment-1362">Timo Oster</a>.</p>
<p>My thoughts exactly.  Based on the Single Resposibilty Princple, the job of optional is to add one more value to the range of types available, not to add new features (such as assigning to references).  The job of making references rebindable is done by reference_wrapper.  You can always combine the two: optional&lt;reference_wrapper&gt; and you have got rebinding behaviour with the null state:</p>
<p>using namespace std;<br />
int i = 42;<br />
int j = 27;<br />
int k = -3;<br />
optional&lt;reference_wrapper&gt; o1 = ref(i);<br />
optional&lt;reference_wrapper&gt; o2 = ref(k);</p>
<p>o1 = ref(j); // o1 now references j.  i, j and j themselves are unchanged<br />
o1 = o2; // o1 now references k.  i, j and k themselves are unchanged<br />
o1-&gt;get() = 84; // updates k<br />
o1 = nullopt; // o1 now references nothing at all.<br />
o1-&gt;get() = -67; // Undefined behaviour &#8211; dereferencing a null optional</p>
<p>This behaves very much like a pointer without the arithmetic, albeit with different syntax, so we don&#8217;t need optional to do that for us.</p>
<p>So if we already can have rebinding behaviour, we should make it assign to the underlying, just as references themselves do.  However, we have to ask ourselves what do we do if either the source or destination is null/unset?   The solution is to disallow assignment except when the source is nullopt_t and force the caller to dereference of the optional (to a T&amp;) to get assignment:</p>
<p>optional o3 = i;<br />
optional o4 = k;</p>
<p>o3 = j; // doesn&#8217;t compile<br />
o3 = o4; // also doesn&#8217;t compile<br />
*o3 = j; // updates i so that i == j<br />
o3 = nullopt; // this _is_ allowed and resets o3<br />
*o3 = k; // Undefined behaviour &#8211; dereferencing a null optional</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Timo Oster		</title>
		<link>https://www.fluentcpp.com/2018/10/05/pros-cons-optional-references/#comment-1362</link>

		<dc:creator><![CDATA[Timo Oster]]></dc:creator>
		<pubDate>Fri, 05 Oct 2018 07:21:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=1853#comment-1362</guid>

					<description><![CDATA[Why not make `optional` consistent with `T&#038;` and disallow rebinding for empty and non-empty optional references alike? 

When talking about consistency, there&#039;s also `std::reference_wrapper` to consider, which is also rebindable, but does not have a separate null state.

I guess it all depends on the use case. Do you want optional references to act like references that can be null (non-rebindable), or like pointers that have an explicit separate null state (rebindable)? There are use cases for both. Maybe that means there have to be two classes with distinct names that make clear what&#039;s happening?]]></description>
			<content:encoded><![CDATA[<p>Why not make `optional` consistent with `T&amp;` and disallow rebinding for empty and non-empty optional references alike? </p>
<p>When talking about consistency, there&#8217;s also `std::reference_wrapper` to consider, which is also rebindable, but does not have a separate null state.</p>
<p>I guess it all depends on the use case. Do you want optional references to act like references that can be null (non-rebindable), or like pointers that have an explicit separate null state (rebindable)? There are use cases for both. Maybe that means there have to be two classes with distinct names that make clear what&#8217;s happening?</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
