-7

When std::make_shared(new Foo()) is called, it constructs a Foo and returns a std::shared_ptr<Foo> for the caller (viz. here). If this is called multiple times from various objects, does it construct a new Foo() each time? In this case is it no different than each caller getting a single reference to a new object, acting in practicality like a unqiue_ptr?

Or does it make one Foo() the first time and then subsequently returning std::shared_ptrs to it, knowing that it will act like a Singleton of sorts (and of course deleting once the last std::shared_ptr is destroyed)? Is this platform specific?

Specifically a function as such:

std::shared_ptr<Foo> makeFoo()
{
  return std::make_shared<Foo>();
}
Danny A
  • 323
  • 1
  • 4
  • 14
  • It constructs a new `Foo` every time it called. – 101010 Aug 06 '14 at 13:10
  • No, it will not create a singleton and it is not the same as a unqiue_ptr. – Mithrandir Aug 06 '14 at 13:11
  • `new Foo()` and `makeFoo()` always creates a new object, so no, a singleton isn't returned. The names unique and shared only refer to the way the class can be used: `shared_ptr`s have reference counting and multiple `shared_ptr`s to the same resource can exist. `unique_ptr`s do the opposite where only one instance of the class has access to the resource. – David G Aug 06 '14 at 13:12
  • Why don't you construct two of them and compare their addresses? – Martin G Aug 06 '14 at 13:12
  • 4
    I'm actually curious as to how you came to think that `make_shared` had anything to do with singletons. There was another question that asked if `make_unique` made singletons, even though the two functions return totally different smart pointers. I'm actually wondering if there is some really badly-written C++11 tutorial somewhere spreading disinformation. – In silico Aug 06 '14 at 13:17
  • There's absolutely no mention of it in the documentation, I'm actually surprised too.. – Marco A. Aug 06 '14 at 13:17
  • Your confusion appears to be intermingling `std::make_shared(Args...)` with `std::shared_ptr(new Type(Args...))` *Neither* create singletons. The power of shared pointers is in their assignment and/or initialization from *other shared pointers*, not just new instances. (and of course, the automatic reference counted memory management is the cat's whiskers). – WhozCraig Aug 06 '14 at 13:18
  • @Insilico I've seen usages of make_shared being wrapped in a makeFoo() function pretty commonly. I haven't used shared_ptrs because Google's C++ style guide suggest trying to design ownership carefully using unique_ptr instead. With makeFoo() I just didn't see why a shared_ptr would be returned if you were going to be the sole owner of a newly created object. Obviously other calls to makeFoo would give you your own new Foo, thus you could just use make_unique instead. If you had a shared_ptr, you would have to pass that reference to other objects, defeating the purpose of a makeFoo function. – Danny A Aug 06 '14 at 13:21

1 Answers1

5

No, std::make_shared<Foo>() will always create a new Foo object and return the managed pointer to it.

The difference to unique_ptr is, that you can have multiple references to your pointer, while unique_ptr has only one living reference to your object.

auto x = std::make_shared<Foo>();
auto y = x; // y and x point to the same Foo
auto x1 = std::make_unique<Foo>();
auto y1 = x1; // Does not work
MatthiasB
  • 1,759
  • 8
  • 18