Note that there are some explanatory texts on larger screens.

plurals
  1. POHow should I assign boost::interprocess::unique_ptr returned from a factory function, possibly using boost::move, in C++03
    primarykey
    data
    text
    <p>I'm trying to create a factory function that would return boost::interprocess::unique_ptr. Here's an example:</p> <pre><code>#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; } </code></pre> <p>The problem is that gcc fails to compile the above code saying:</p> <pre><code>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] </code></pre> <p>When I change</p> <pre><code>x = create(); </code></pre> <p>to</p> <pre><code>x = boost::move(create()); </code></pre> <p>then gcc says:</p> <pre><code>main.cpp:22: error: invalid initialization of non-const reference of type ‘uptr&amp;’ from a temporary of type ‘uptr’ ../../boost_latest/boost/move/move.hpp:330: error: in passing argument 1 of ‘typename boost::move_detail::enable_if&lt;boost::has_move_emulation_enabled&lt;T&gt;, boost::rv&lt;T&gt;&amp;&gt;::type boost::move(T&amp;) [with T = uptr]’ </code></pre> <p>Am I doing something wrong?</p> <p>Interestingly, when I do:</p> <pre><code>uptr x2 = create(); </code></pre> <p>the code compiles without any issues.</p> <p>BTW: I use gcc v4.4.3 and Boost v1.51.0.</p> <hr> <p><strong>UPDATE:</strong></p> <p>I've been able to overcome this issue by using the following snippet:</p> <pre><code>x = static_cast&lt;boost::rv&lt;uptr&gt;&amp;&gt;(create()); </code></pre> <p>The above cast is based on the first version of ambiguous overload for <code>operator=</code> mentioned in the original question. The second one (<code>operator=(int boost::interprocess::unique_ptr&lt;T, D&gt;::nat::*</code>) is probably provided by the implementation to emulate <code>std::unique_ptr::operator=(nullptr_t)</code>, which as a matter of fact resets the <code>unique_ptr</code>. It turns out, it also makes <code>operator=</code> ambiguous.</p> <p>Unfortunately, using the above-mentioned <code>static_cast&lt;&gt;()</code> makes using my factory too much complicated.</p> <p>One way to solve this problem would be to remove the second overload for <code>operator=</code>, as one can always explicitly call <code>unique_ptr::reset()</code>.</p> <p>Still, I'm wondering if and how <code>boost::move()</code> could help me with this issue.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload