#7598 closed Bugs (fixed)
Unable to use operator=() for boost::interprocess::unique_ptr due to its ambiguity
Reported by: | Owned by: | Ion Gaztañaga | |
---|---|---|---|
Milestone: | To Be Determined | Component: | interprocess |
Version: | Boost 1.51.0 | Severity: | Problem |
Keywords: | Cc: |
Description
I'm not able to easily use operator=()
of boost::interprocess::unique_ptr
. See the example below:
#include <boost/interprocess/smart_ptr/unique_ptr.hpp> using namespace boost::interprocess; class my_class { public: my_class() {} }; struct my_class_deleter { void operator()(my_class *p) {} }; typedef unique_ptr<my_class, my_class_deleter> uptr; uptr create() { return uptr(); } int main() { uptr x; x = create(); return 0; }
The problem is that gcc fails to compile the above code saying:
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<T, D>& boost::interprocess::unique_ptr<T, D>::operator=(boost::rv<boost::interprocess::unique_ptr<T, D> >&) [with T = my_class, D = my_class_deleter] ../../boost_latest/boost/interprocess/smart_ptr/unique_ptr.hpp:249: note: boost::interprocess::unique_ptr<T, D>& boost::interprocess::unique_ptr<T, D>::operator=(int boost::interprocess::unique_ptr<T, D>::nat::*) [with T = my_class, D = my_class_deleter]
Now, when I change the main()
function to something like this:
int main() { uptr x = create(); return 0; }
the code compiles without any issues.
I've been able to overcome this issue by using the following snippet:
x = static_cast<boost::rv<uptr>&>(create());
It works, but it's not an elegant solution. I'm not even sure if it's a legal construct.
I've also investigated boost::interprocess::unique_ptr
implementation and I think I've found the problem. The conversion-to-bool operator messes up with the operator=()
.
I attached a patch that fixes this problem, however I'm not sure if it doesn't break any existing code.
BTW: I use gcc v4.4.3 and Boost v1.51.0 on Ubuntu 10.04.
BTW 2: Before I managed to overcome this issue I had written about it on stackoverflow.com: http://stackoverflow.com/questions/13086428/how-should-i-assign-boostinterprocessunique-ptr-returned-from-a-factory-func
Attachments (4)
Change History (8)
by , 10 years ago
Attachment: | unique_ptr.patch added |
---|
by , 10 years ago
by , 10 years ago
Attachment: | unique_ptr.2.patch added |
---|
comment:1 by , 10 years ago
Thanks for the report. To fix this we declare another pointer type to emulate nullptr but without conversion operator from unique_ptr.
comment:3 by , 10 years ago
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.
Patch to fix the ambiguity