0

As I understand it, if I have a class that has a boost::scoped_ptr member variable, and if I were to copy an instance of that class and would like the new instance to have its scoped_ptr member point to a copy of what the first instance pointed to, I would have to implement a custom copy constructor (and assignment operator) and make the deep copying explicitly of whatever the scoped_ptr points to. It would be great if the smart pointer type had a copy constructor that did this itself.

Are there no similar smart pointers in c++ boost / stl libraries that already has the deep copy functionality built in, so that when the smart pointer is copied the object it points to is also copied? At least as an option?

(If there was such a pointer, in my case the pointer would have to know a bit more about how it should create the new object since my objects pointed to are of polymorphic classes, with virtual Clone() functions. If the smart pointer couldn't implicitly figure out how to deep copy, the client code could have supplied a function pointer or something pointing to the Clone function or whatever factory creation function should be used. I guess this complication of how the new object could created that might be one of the reasons why no pointer has the deep copy functionality after all..?)

shadow_map
  • 313
  • 3
  • 15
  • `std::unique_ptr`'s content isn't copied, but just handed around among owners. – πάντα ῥεῖ Jul 27 '16 at 05:57
  • 1
    Yes. That's what I don't want. – shadow_map Jul 27 '16 at 06:00
  • Don't use pointers then, or provide a `clone()` function for your classes. – πάντα ῥεῖ Jul 27 '16 at 06:01
  • 2
    I don't think you've understood the question. I already have that. – shadow_map Jul 27 '16 at 06:02
  • 1
    No, I didn't understand your question. It's actually unclear what you're asking about. – πάντα ῥεῖ Jul 27 '16 at 06:03
  • 3
    There once was a proposal for a [`value_ptr`](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3339.pdf). I'm not sure what happened to it, but there are other implementations lying around. – juanchopanza Jul 27 '16 at 06:03
  • @πάνταῥεῖ My polymorphic classes has clone() functions. I have another class "A" (not part of that polymorphic inheritance tree) that need to have a scoped_ptr as a member. When I clone "A" I want what scoped_ptr points to be cloned without having to implement copy constructor in "A". – shadow_map Jul 27 '16 at 06:03
  • 4
    @πάνταῥεῖ a smart pointer with value semantics? – juanchopanza Jul 27 '16 at 06:04
  • @juanchopanza Exactly. Would be great if such functionality was included in standard libraries. – shadow_map Jul 27 '16 at 06:19
  • ps. Found a little library called [Aurora](http://bromeon.ch/libraries/aurora/tutorials/v1.0/smartptr.html) which has a CopiedPtr that is pretty much what I was asking for. – shadow_map Jul 27 '16 at 07:20
  • I am curious. With the `clone()` function already existing, what would be the problem with whipping up a copy constructor for the class? Why looking at the smart pointer implementation for this functionality? – DevSolar Jul 27 '16 at 07:26
  • @DevSolar Yup, that's what I've done. I just think it's tedious to put up the copy constructor and assignment operator for only that reason (since I then have to explicitly copy the other members as well, making it easier to do mistakes). Would be great with a smart pointer with value semantics since it could be used for holding class scoped polymorphic objects enabling RAII pattern without extra code. – shadow_map Jul 27 '16 at 07:39

1 Answers1

4

As far as I know, no smart pointer in the standard library implements the semantics you want.

However, the MNMLSTC core library provides – among other things – two smart pointers (namely, core::poly_ptr and core::deep_ptr) that should fit your use case. In particular, because of the presence of virtual member functions, I believe core::poly_ptr is what you need.

Ilio Catallo
  • 3,152
  • 2
  • 22
  • 40