How to make multi_index_container work with std::unique_ptr?

Hi All, If the elements of a multi_index_container are std::unique_ptr, it doesn't seem to work because the assignment operator of std::unique_ptr is private. Any way to let it work? If I change unique_ptr to shared_ptr, it works. But I need a good performance. Or is there anyway to work around to get pointer container performance with multi index power? Thanks a lot! Jayden

If the elements of a multi_index_container are std::unique_ptr, it doesn't seem to work because the assignment operator of std::unique_ptr is private. Any way to let it work? If I change unique_ptr to shared_ptr, it works. But I need a good performance. Or is there anyway to work around to get pointer container performance with multi index power?
Why do you think it would affect your program performance? Have you run profiler, or it's just a guess? I use MIC with shared_ptr a lot and it never was a bottleneck.

It is just my guess. In fact I need MIC with shared_ptr to a scoped_ptr.
The shared_ptr is used to count number of uses, while the scoped_ptr used
to handle polymorphic objects. I feel a little nervous with that.
On Tue, Dec 20, 2011 at 2:14 PM, Igor R
If the elements of a multi_index_container are std::unique_ptr, it doesn't seem to work because the assignment operator of std::unique_ptr is private. Any way to let it work? If I change unique_ptr to shared_ptr, it works. But I need a good performance. Or is there anyway to work around to get pointer container performance with multi index power?
Why do you think it would affect your program performance? Have you run profiler, or it's just a guess? I use MIC with shared_ptr a lot and it never was a bottleneck. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Tuesday, December 20, 2011 2:54 PM, Jayden Shui wrote:
It is just my guess. In fact I need MIC with shared_ptr to a scoped_ptr. The shared_ptr is used to count number of uses, while the scoped_ptr used to handle polymorphic objects. I feel a little nervous with that.
There should be no need for both pointer types. Shared_ptr handles polymorphic objects just fine. A MIC holding shared_ptrs to your base class should do what you need, and the overhead associated with the shared_ptr should be negligible.

My case is like that: I have a number of polymorphic objects which can be used or referenced by other objects. I put them (the polymorphic objects) in MIC. And if one changes its type (not only its value), its users know it automatically by shared_ptr. I think I need MIC with shared_ptr pointing to scoped_ptr. Am I right? Or is there other way to easy handle it? Thanks a lot. On Tue, Dec 20, 2011 at 3:02 PM, Andrew Holden < aholden@charteroaksystems.com> wrote:
On Tuesday, December 20, 2011 2:54 PM, Jayden Shui wrote:
It is just my guess. In fact I need MIC with shared_ptr to a scoped_ptr. The shared_ptr is used to count number of uses, while the scoped_ptr used to handle polymorphic objects. I feel a little nervous with that.
There should be no need for both pointer types. Shared_ptr handles polymorphic objects just fine.
A MIC holding shared_ptrs to your base class should do what you need, and the overhead associated with the shared_ptr should be negligible. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Please don't top-post.
My case is like that: I have a number of polymorphic objects which can be used or referenced by other objects. I put them (the polymorphic objects) in MIC. And if one changes its type (not only its value), its users know it automatically by shared_ptr. I think I need MIC with shared_ptr pointing to scoped_ptr. Am I right? Or is there other way to easy handle it?
How can an object change its type?! In c++ type is a compile-type attribute. Perhaps you mean that you erase an object from the container and add another one, or replace some object. In any case, shared_ptr would be fine.

On Tue, Dec 20, 2011 at 5:23 PM, Igor R
Please don't top-post.
My case is like that: I have a number of polymorphic objects which can be used or referenced by other objects. I put them (the polymorphic objects) in MIC. And if one changes its type (not only its value), its users know it automatically by shared_ptr. I think I need MIC with shared_ptr pointing to scoped_ptr. Am I right? Or is there other way to easy handle it?
How can an object change its type?! In c++ type is a compile-type attribute. Perhaps you mean that you erase an object from the container and add another one, or replace some object. In any case, shared_ptr would be fine. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
What I mean is dynamic changing. For example
class B {public: virtual ~B()} // Base class.
class D1 : public B {} // D1 derived from B
class D2 : public B {} // D1 derived from B
scoped_ptr<B> p = new D1; // pointing to D1
p.reset(new D2); // change pointing to D2
If they are shared between owners and users, I need type
shared_ptr

What I mean is dynamic changing. For example
class B {public: virtual ~B()} // Base class. class D1 : public B {} // D1 derived from B class D2 : public B {} // D1 derived from B
scoped_ptr<B> p = new D1; // pointing to D1 p.reset(new D2); // change pointing to D2
If they are shared between owners and users, I need type
shared_ptr
But this type make my code hard to develop and also read.
And what's wrong with just shared_ptr<B>? Why do you need the additional level of indirection?

On Tue, Dec 20, 2011 at 6:01 PM, Igor R
What I mean is dynamic changing. For example
class B {public: virtual ~B()} // Base class. class D1 : public B {} // D1 derived from B class D2 : public B {} // D1 derived from B
scoped_ptr<B> p = new D1; // pointing to D1 p.reset(new D2); // change pointing to D2
If they are shared between owners and users, I need type
shared_ptr
But this type make my code hard to develop and also read.
And what's wrong with just shared_ptr<B>? Why do you need the additional level of indirection?
For example
int main() { shared_ptr<B> p(new D1); shared_ptr<B> user1 = p; // User1 uses p p.reset(new D2); // p changes pointed type to D2, user1 still points to D1 return 0; } or int main() { shared_ptr<B> p(new D1); shared_ptr<B>* user2 = &p; // User2 uses p, but the p.use_count() is still 1. p.reset(new D2); // p changes pointed type to D2, *user2 points to D1 return 0; } Thanks.

And what's wrong with just shared_ptr<B>? Why do you need the additional level of indirection?
For example
int main() { shared_ptr<B> p(new D1); shared_ptr<B> user1 = p; // User1 uses p p.reset(new D2); // p changes pointed type to D2, user1 still points to D1 return 0; }
or
int main() { shared_ptr<B> p(new D1); shared_ptr<B>* user2 = &p; // User2 uses p, but the p.use_count() is still 1. p.reset(new D2); // p changes pointed type to D2, *user2 points to D1 return 0; }
Ok, so you actually want the following: 1) The container itself shouldn't have "strong" references to your objects 2) An object can be recreated/replaced when it doesn't have any strong references. Am I right? If so, store weak_ptr<Base> in your container: // pseudo-code int main() { multi_index_container< weak_ptr<B>, // some indices
container; shared_ptr<B> p(new D1); container.insert(p);
// at some futher stage: auto iterator = container.find(someKey); auto p = iterator->lock(); // if the weak_ptr expired, i.e. no other clients use object, you may re-create it if (!p) recreateIt(); } Note, however, that there's one pitfall here: if weak_ptr expiration affects the keys, you'll have to "refresh" MIC indices somehow.

On Wed, Dec 21, 2011 at 4:54 AM, Igor R
And what's wrong with just shared_ptr<B>? Why do you need the additional level of indirection?
For example
int main() { shared_ptr<B> p(new D1); shared_ptr<B> user1 = p; // User1 uses p p.reset(new D2); // p changes pointed type to D2, user1 still points to D1 return 0; }
or
int main() { shared_ptr<B> p(new D1); shared_ptr<B>* user2 = &p; // User2 uses p, but the p.use_count() is still 1. p.reset(new D2); // p changes pointed type to D2, *user2 points to D1 return 0; }
Ok, so you actually want the following: 1) The container itself shouldn't have "strong" references to your objects 2) An object can be recreated/replaced when it doesn't have any strong references. Am I right?
If so, store weak_ptr<Base> in your container:
// pseudo-code int main() { multi_index_container< weak_ptr<B>, // some indices
container; shared_ptr<B> p(new D1); container.insert(p);
// at some futher stage: auto iterator = container.find(someKey); auto p = iterator->lock(); // if the weak_ptr expired, i.e. no other clients use object, you may re-create it if (!p) recreateIt(); }
Hi, Igore,
I'd like to first thank you for keeping helping me one this problem. I'd
like to try to make my requirements clear by some pseudo codes.
int main()
{
multi_index_container<
shared_ptr
container; // container own the objects.
// Insert object M of type D1 into container
container.insert(shared_ptr
participants (3)
-
Andrew Holden
-
Igor R
-
Jayden Shui