Boost C++ Libraries: Ticket #3395: Assignment to optional is not possible if operator& is overriden https://svn.boost.org/trac10/ticket/3395 <p> The assignment operator for optional&lt; T &gt; uses T::operator&amp; to dispatch the call between assign_expr functions. This dispatch breaks if the T::operator&amp; returns something else than T*. This is the case with ATL types, such as CComBSTR. </p> <p> The suggested fix is to replace call to operator&amp; with addressof. Please, find the attached patch against the 1.40 release. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3395 Trac 1.4.3 Andrey Semashev Thu, 03 Sep 2009 07:02:43 GMT <link>https://svn.boost.org/trac10/ticket/3395#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3395#comment:1</guid> <description> <p> BTW, I just now noticed that the assignment operator accepts its argument by value. It may be worth to accept it by const reference, as the operator is not only called for in-place factories but also for other types. For example: </p> <pre class="wiki">struct MyString { MyString(std::string const&amp;); }; std::string str = "Hello"; boost::optional&lt; MyString &gt; opt; opt = str; // here a needless copy of str will be constructed </pre><p> I'm attaching an updated patch that fixes this, too. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Thu, 03 Sep 2009 07:04:03 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/3395 https://svn.boost.org/trac10/ticket/3395 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">optional.hpp.patch</span> </li> </ul> <p> An updated fix that also optimises away copying the assignment argument </p> Ticket Andrey Semashev Sun, 05 Dec 2010 14:43:21 GMT <link>https://svn.boost.org/trac10/ticket/3395#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3395#comment:2</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/67020" title="Refs #3395. Optional construction and assignment now works correctly ...">[67020]</a>) Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3395" title="#3395: Bugs: Assignment to optional is not possible if operator&amp; is overriden (closed: fixed)">#3395</a>. Optional construction and assignment now works correctly for types with overridden operator&amp;. Also silenced some GCC warnings about broken strict aliasing rules. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ralf W. Grosse-Kunstleve</dc:creator> <pubDate>Wed, 08 Dec 2010 02:57:48 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/3395 https://svn.boost.org/trac10/ticket/3395 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">zd</span> </li> </ul> <p> proposed patch for review </p> Ticket Ralf W. Grosse-Kunstleve Wed, 08 Dec 2010 03:01:47 GMT <link>https://svn.boost.org/trac10/ticket/3395#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3395#comment:3</guid> <description> <p> The current optional.hpp leads to gcc 3.2 warnings (Redhat 8.0). It is easily fixed with the proposed patch I attached a minute ago. Is this OK to commit? -- It is tested already locally on a large number of platforms. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Wed, 08 Dec 2010 16:52:42 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3395#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3395#comment:4</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/3395#comment:3" title="Comment 3">rwgk</a>: </p> <blockquote class="citation"> <p> The current optional.hpp leads to gcc 3.2 warnings (Redhat 8.0). It is easily fixed with the proposed patch I attached a minute ago. Is this OK to commit? -- It is tested already locally on a large number of platforms. </p> </blockquote> <p> This looks good to me. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Wed, 08 Dec 2010 17:16:52 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3395#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3395#comment:5</guid> <description> <p> Looks good to me as well. Please go ahead and commit. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Wed, 08 Dec 2010 17:50:07 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3395#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3395#comment:6</guid> <description> <p> Perhaps, the patch should also take into account GCC versions prior to 3.2? I really don't know since which version may_alias has become available. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ralf W. Grosse-Kunstleve</dc:creator> <pubDate>Wed, 08 Dec 2010 18:01:25 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3395#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3395#comment:7</guid> <description> <p> I checked in my patch as svn trunk rev. <a class="changeset" href="https://svn.boost.org/trac10/changeset/67109" title="boost/optional/optional.hpp: avoid gcc 3.2 warnings">[67109]</a> before seeing comment 6. </p> <p> Is anyone still using a gcc before 3.2? Even 3.2 is ancient now (released 2002). It is the oldest gcc I have, i.e. I couldn't test with any older versions. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Wed, 08 Dec 2010 18:52:18 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3395#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3395#comment:8</guid> <description> <p> That's ok, I've updated your change with a check for earlier GCC versions. Try with the updated version. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 09 Dec 2010 16:23:01 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/3395#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3395#comment:9</guid> <description> <p> It ran without a problem in our nightly builds (24 platforms). Thanks! </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Andrey Semashev</dc:creator> <pubDate>Sun, 12 Dec 2010 11:34:16 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/3395#comment:10 https://svn.boost.org/trac10/ticket/3395#comment:10 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/67183" title="Merged changes from trunk. Fixes #3395. Also updates swap behavior: if ...">[67183]</a>) Merged changes from trunk. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3395" title="#3395: Bugs: Assignment to optional is not possible if operator&amp; is overriden (closed: fixed)">#3395</a>. Also updates swap behavior: if default constructor has no-throw guarantee, swap will use it to provide no-throw guarantee itself. operator&gt;&gt; behavior changed slightly so that the stream is not accessed when unrecognized character sequence is detected. The stream is marked with failbit in such a case. </p> Ticket