Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#7598 closed Bugs (fixed)

Unable to use operator=() for boost::interprocess::unique_ptr due to its ambiguity

Reported by: Adam Romanek <romanek.adam@…> 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)

unique_ptr.patch (837 bytes ) - added by Adam Romanek <romanek.adam@…> 10 years ago.
test.cpp (357 bytes ) - added by Adam Romanek <romanek.adam@…> 10 years ago.
unique_ptr.2.patch (1.5 KB ) - added by Ion Gaztañaga 10 years ago.
Patch to fix the ambiguity
unique_ptr.3.patch (1.5 KB ) - added by Ion Gaztañaga 10 years ago.
Corrected patch

Download all attachments as: .zip

Change History (8)

by Adam Romanek <romanek.adam@…>, 10 years ago

Attachment: unique_ptr.patch added

by Adam Romanek <romanek.adam@…>, 10 years ago

Attachment: test.cpp added

by Ion Gaztañaga, 10 years ago

Attachment: unique_ptr.2.patch added

Patch to fix the ambiguity

comment:1 by Ion Gaztañaga, 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.

by Ion Gaztañaga, 10 years ago

Attachment: unique_ptr.3.patch added

Corrected patch

comment:2 by Ion Gaztañaga, 10 years ago

Resolution: fixed
Status: newclosed

(In [82537]) Fixes #7598

comment:3 by teeks99@…, 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.

comment:4 by Ion Gaztañaga, 10 years ago

(In [82778]) Fixes #7598

Note: See TracTickets for help on using tickets.