7

As output from one function, I get an object of type Foo. As an argument to another class, I need to pass an object of type std::shared_ptr<Foo>. How can I make the shared pointer from the original object?

2
  • Can you change the function returning Foo or the function taking shared_ptr<Foo>, or are they both from some third-party library or something? (And, if so, maybe if you tell us which one, we can skim the docs and figure out what the intent is.)
    – abarnert
    Commented Aug 16, 2012 at 17:34
  • And this is why taking shared_ptr arguments is suboptimal and should be avoided. Commented Aug 16, 2012 at 17:49

5 Answers 5

9

This is really quite simple:

auto val = std::make_shared<Foo>(FuncThatReturnsFoo(...));

Basically, just heap allocate a new Foo, copying/moving the result into it.

3
  • 2
    shouldn't that be std::make_shared<Foo>(FuncThatReturnsFoo(...))?
    – josefx
    Commented Aug 16, 2012 at 17:22
  • 1
    I think the template parameter to make_shared needs to be explicit. make_shared<Foo>(FuncThatReturnsFoo(...)).
    – bames53
    Commented Aug 16, 2012 at 17:33
  • @bames53: I was going to fix it, but Jonathan got there first. Commented Aug 16, 2012 at 17:45
5

I wouldn't do that if I were you. Mainly because std::shared_ptr manages memory, whereas if you get an object as a return type, the memory is managed automatically (usually, and most likely).

You'll either have to create a new object in dynamic storage

Foo getObject();
//...
std::shared_ptr<Foo> ptr(new Foo(getObject())); //construct new object in dynamic memory
                                                //using the copy constructor

or change the function to return a pointer to an object whose memory you manage.

Foo getObject(); 
//becomes
Foo* getObject();
//or, even better
std::shared_ptr<Foo> getObject();
2

There are two ways to do this:

  1. Create a new copy on the heap and make a shared_ptr from that.
  2. Make a shared_ptr from it with a null deleter.

The first can be done by writing

auto newPtr = std::make_shared<Foo>( foo );

This works, if the class Foo is copy-constructable. The second thing can be accomplished by

auto newPtr = std::shared_ptr<Foot>( &foo, [](void*){} );

Here you don't create a new copy of the object. However, this is only safe, if you can guarantee that the pointer is not accessed, after foo goes out of scope. Otherwise, you will access a destroyed object and you program is likely to do random stuff.

1

I suspect you want to prevent heap-allocation otherwise just heap-allocate a copy of the returned object and go ahead.

You need to prevent that the deleter actually deletes something and need the stack to take care of this:

// given the signatures
Foo f();
void other(std::shared_ptr<Foo> x);

Foo my_f = f();
std::shared_ptr<Foo> my_fptr{&my_f, [](Foo*) {}};
other(my_fptr);

That is a real code smell, though. Why would a function accept a shared_ptr if not to extend life-time?

0

You can do this

std::shared_ptr<Foo> ptr(new Foo(f());

Semantically, it's making a copy of the return value, but the copy constructor should be elided.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.