Boost C++ Libraries: Ticket #10839: dereferencing of rvalue optional<T&> fails due to invalid assignment of rvalue to non-const ref https://svn.boost.org/trac10/ticket/10839 <p> Compiling the following code using <code>g++-4.9.2 -std=gnu++1y</code> </p> <pre class="wiki">#include &lt;boost/optional.hpp&gt; int main(int, char const **) { int i (0); boost::optional&lt;int &amp;&gt; opt (i); // we use std::move here to force opt to be an rvalue. The same error // is triggered if the optional is returned from a function call return * std::move(opt); } </pre><p> results in the error message </p> <blockquote class="citation"> <p> lib/boost/install/include/boost/optional/optional.hpp:1010:89: error: invalid initialization of non-const reference of type 'boost::optional&lt;int&amp;&gt;::reference_type_of_temporary_wrapper {aka int&amp;}' from an rvalue of type 'boost::move_detail::remove_reference&lt;int&amp;&gt;::type {aka int}' </p> </blockquote> <p> Looking at the source code in <code>optional.hpp</code>, the culprit seems to be: </p> <pre class="wiki">template &lt;class T&gt; class optional : public optional_detail::optional_base&lt;T&gt; { // ... reference_type_of_temporary_wrapper operator *() &amp;&amp; { return boost::move(this-&gt;get()) ; } // ... }; </pre><p> <code>reference_type_of_termporary_wrapper</code> is typedefed to <code>int &amp;</code> as is to be expected. If I am not mistaken, the <code>std::move</code> call however is not correct in this case: it converts it's <code>int &amp;</code> argument (the result of <code>this-&gt;get()</code>) to <code>int &amp;&amp;</code> which is <em>not</em> implicitly convertible to <code>int &amp;</code> (the return type). </p> <p> This problem only occurs with <code>optional&lt;T&amp;&gt;</code> not with <code>optional&lt;T const &amp;&gt;</code> since for the latter, <code>reference_type_of_temporary_wrapper</code> is <code>int const &amp;</code> and <code>int &amp;&amp;</code> is convertible to <code>int const &amp;</code>. </p> <p> Possible resolution: If the type of the optional is a reference type, don't apply <code>std::move</code> (e.g. add a static <code>move</code> member function to <code>types_when_isnt_ref</code> and <code>types_when_is_ref</code> and use that member instead of <code>boost::move</code> here). </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/10839 Trac 1.4.3 stefa Wed, 03 Dec 2014 08:40:12 GMT attachment set https://svn.boost.org/trac10/ticket/10839 https://svn.boost.org/trac10/ticket/10839 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">optional.hpp.diff</span> </li> </ul> Ticket anonymous Wed, 03 Dec 2014 08:41:09 GMT <link>https://svn.boost.org/trac10/ticket/10839#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10839#comment:1</guid> <description> <p> I just realized, that such a <code>move</code> member is already defined, it's just not used. I have attached a patch which solves the problem for me. </p> </description> <category>Ticket</category> </item> <item> <author>stefan.bund@…</author> <pubDate>Wed, 03 Dec 2014 08:56:41 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/10839 https://svn.boost.org/trac10/ticket/10839 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">optional.hpp.2.diff</span> </li> </ul> <p> correct version of patch </p> Ticket viboes Fri, 13 Feb 2015 18:30:52 GMT component changed; owner set https://svn.boost.org/trac10/ticket/10839#comment:2 https://svn.boost.org/trac10/ticket/10839#comment:2 <ul> <li><strong>owner</strong> set to <span class="trac-author">Fernando Cacciola</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">optional</span> </li> </ul> Ticket akrzemi1 Mon, 09 Mar 2015 09:38:27 GMT owner, status changed https://svn.boost.org/trac10/ticket/10839#comment:3 https://svn.boost.org/trac10/ticket/10839#comment:3 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Fernando Cacciola</span> to <span class="trac-author">akrzemi1</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket akrzemi1 Mon, 23 Mar 2015 20:38:49 GMT status, milestone changed; resolution set https://svn.boost.org/trac10/ticket/10839#comment:4 https://svn.boost.org/trac10/ticket/10839#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.58.0</span> </li> </ul> <p> Value accessors in rvalues of optional references now work correctly. </p> Ticket