Boost C++ Libraries: Ticket #7598: Unable to use operator=() for boost::interprocess::unique_ptr due to its ambiguity https://svn.boost.org/trac10/ticket/7598 <p> I'm not able to easily use <code>operator=()</code> of <code>boost::interprocess::unique_ptr</code>. See the example below: </p> <pre class="wiki">#include &lt;boost/interprocess/smart_ptr/unique_ptr.hpp&gt; using namespace boost::interprocess; class my_class { public: my_class() {} }; struct my_class_deleter { void operator()(my_class *p) {} }; typedef unique_ptr&lt;my_class, my_class_deleter&gt; uptr; uptr create() { return uptr(); } int main() { uptr x; x = create(); return 0; } </pre><p> The problem is that gcc fails to compile the above code saying: </p> <pre class="wiki">main.cpp:22: error: ambiguous overload for ‘operator=’ in ‘x = create()()’ ../../boost_latest/boost/interprocess/smart_ptr/unique_ptr.hpp:211: note: candidates are: boost::interprocess::unique_ptr&lt;T, D&gt;&amp; boost::interprocess::unique_ptr&lt;T, D&gt;::operator=(boost::rv&lt;boost::interprocess::unique_ptr&lt;T, D&gt; &gt;&amp;) [with T = my_class, D = my_class_deleter] ../../boost_latest/boost/interprocess/smart_ptr/unique_ptr.hpp:249: note: boost::interprocess::unique_ptr&lt;T, D&gt;&amp; boost::interprocess::unique_ptr&lt;T, D&gt;::operator=(int boost::interprocess::unique_ptr&lt;T, D&gt;::nat::*) [with T = my_class, D = my_class_deleter] </pre><p> Now, when I change the <code>main()</code> function to something like this: </p> <pre class="wiki">int main() { uptr x = create(); return 0; } </pre><p> the code compiles without any issues. </p> <p> I've been able to overcome this issue by using the following snippet: </p> <pre class="wiki">x = static_cast&lt;boost::rv&lt;uptr&gt;&amp;&gt;(create()); </pre><p> It works, but it's not an elegant solution. I'm not even sure if it's a legal construct. </p> <p> I've also investigated <code>boost::interprocess::unique_ptr</code> implementation and I think I've found the problem. The conversion-to-bool operator messes up with the <code>operator=()</code>. </p> <p> I attached a patch that fixes this problem, however I'm not sure if it doesn't break any existing code. </p> <p> BTW: I use gcc v4.4.3 and Boost v1.51.0 on Ubuntu 10.04. </p> <p> BTW 2: Before I managed to overcome this issue I had written about it on stackoverflow.com: <a class="ext-link" href="http://stackoverflow.com/questions/13086428/how-should-i-assign-boostinterprocessunique-ptr-returned-from-a-factory-func"><span class="icon">​</span>http://stackoverflow.com/questions/13086428/how-should-i-assign-boostinterprocessunique-ptr-returned-from-a-factory-func</a> </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7598 Trac 1.4.3 Adam Romanek <romanek.adam@…> Mon, 29 Oct 2012 08:41:07 GMT attachment set https://svn.boost.org/trac10/ticket/7598 https://svn.boost.org/trac10/ticket/7598 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">unique_ptr.patch</span> </li> </ul> Ticket Adam Romanek <romanek.adam@…> Mon, 29 Oct 2012 08:41:26 GMT attachment set https://svn.boost.org/trac10/ticket/7598 https://svn.boost.org/trac10/ticket/7598 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test.cpp</span> </li> </ul> Ticket Ion Gaztañaga Fri, 18 Jan 2013 21:02:19 GMT attachment set https://svn.boost.org/trac10/ticket/7598 https://svn.boost.org/trac10/ticket/7598 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">unique_ptr.2.patch</span> </li> </ul> <p> Patch to fix the ambiguity </p> Ticket Ion Gaztañaga Fri, 18 Jan 2013 21:03:24 GMT <link>https://svn.boost.org/trac10/ticket/7598#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7598#comment:1</guid> <description> <p> Thanks for the report. To fix this we declare another pointer type to emulate nullptr but without conversion operator from unique_ptr. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Fri, 18 Jan 2013 21:05:10 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/7598 https://svn.boost.org/trac10/ticket/7598 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">unique_ptr.3.patch</span> </li> </ul> <p> Corrected patch </p> Ticket Ion Gaztañaga Fri, 18 Jan 2013 21:12:06 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/7598#comment:2 https://svn.boost.org/trac10/ticket/7598#comment:2 <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/82537" title="Fixes #7598">[82537]</a>) Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/7598" title="#7598: Bugs: Unable to use operator=() for boost::interprocess::unique_ptr due to ... (closed: fixed)">#7598</a> </p> Ticket teeks99@… Wed, 06 Feb 2013 16:06:18 GMT <link>https://svn.boost.org/trac10/ticket/7598#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7598#comment:3</guid> <description> <p> I think there was a bug created by this fix in unique_ptr.hpp line 191. It is using the for_bool_ from the 2nd patch, not for_bool from the 3rd patch. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Thu, 07 Feb 2013 20:00:44 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7598#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7598#comment:4</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/82778" title="Fixes #7598">[82778]</a>) Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/7598" title="#7598: Bugs: Unable to use operator=() for boost::interprocess::unique_ptr due to ... (closed: fixed)">#7598</a> </p> </description> <category>Ticket</category> </item> </channel> </rss>