Modify

Ticket #7598 (closed Bugs: fixed)

Opened 18 months ago

Last modified 15 months ago

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

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

unique_ptr.patch Download (837 bytes) - added by Adam Romanek <romanek.adam@…> 18 months ago.
test.cpp Download (357 bytes) - added by Adam Romanek <romanek.adam@…> 18 months ago.
unique_ptr.2.patch Download (1.5 KB) - added by igaztanaga 15 months ago.
Patch to fix the ambiguity
unique_ptr.3.patch Download (1.5 KB) - added by igaztanaga 15 months ago.
Corrected patch

Change History

Changed 18 months ago by Adam Romanek <romanek.adam@…>

Changed 18 months ago by Adam Romanek <romanek.adam@…>

Changed 15 months ago by igaztanaga

Patch to fix the ambiguity

comment:1 Changed 15 months ago by igaztanaga

Thanks for the report. To fix this we declare another pointer type to emulate nullptr but without conversion operator from unique_ptr.

Changed 15 months ago by igaztanaga

Corrected patch

comment:2 Changed 15 months ago by igaztanaga

  • Status changed from new to closed
  • Resolution set to fixed

(In [82537]) Fixes #7598

comment:3 Changed 15 months ago by teeks99@…

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 Changed 15 months ago by igaztanaga

(In [82778]) Fixes #7598

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.