0

What does std::make_shared<Object>("foo") do?

std::shared_ptr<Object> p1 = std::make_shared<Object>("foo");

I know that std::make_shared<T> is template class and I could understand Object("foo") is a temporary object indeed. But I could not understand std::make_shared<Object>("foo").

Can someone explain me, step by step, sequence of objects created and operations done by it?

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • 2
    `std::make_shared()` is not a template class. It is a helper function that (1) dynamically allocates a `T` that is initialised (constructed) using the supplied arguments (any arguments - of which there may be zero or more - between the `()`) and (2) constructs and returns a `std::shared_ptr` to manage that object. – Peter May 24 '20 at 06:09
  • Isn't `std::make_shared()` a template function? –  May 24 '20 at 06:18
  • Yes, it is a template function. You describe it as a templated class, which it is not. – Peter May 24 '20 at 07:09
  • I see, i am sorry for disturbing you.I would do my best to avoid it. –  May 24 '20 at 07:10
  • @john I think your comments are helpful.Did you delete them just now?Or just because we have simmilar names? –  May 24 '20 at 07:13

2 Answers2

3

std::make_shared is a template function, not a template class.

The function's template parameter specifies the type to be created.

The function's parameters are passed as-is to that type's constructor.

The function returns a std::shared_ptr that holds a pointer to the type created.

So, std::make_shared<Object>("foo") dynamically creates an Object instance constructed with "foo", and returns a std::shared_ptr<Object> holding a pointer to that instance.

This is equivalent to the following (but not quite the same, as there are additional optimizations involved behind the scenes):

std::shared_ptr<Object> p1(new Object("foo"));
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Could me tell me where i can find the related source code? –  May 24 '20 at 06:22
  • Start in the header file `` although the code may or may not be located in that file. – john May 24 '20 at 06:37
  • @john Btw,isn't `std::make_shared()` a template function?Peter says that it is a helper function.I am confused. –  May 24 '20 at 06:44
  • It is both a template function and a helper function. Helper function is not an official C++ term. – john May 24 '20 at 06:59
1
std::make_shared<Object>("foo")

method

Notice the std::make_shared at the start of this code. It identifies a function. If we put the actual function aside and step back a little, then before the <> signs and the () sign you have a function name.

generic

The in this code specifies that the generic T is specified in this case to be Object. It's not a good idea to think of it as if it was a temporary object. It's a specification.

the call

("foo") is ensuring that the function is called and a parameter is passed to it.

bottom line

The documentation is providing you a proper description of this:

enter image description here

It tells you that the constructor of the given type specified (Object in this case) will be instantiated and will be wrapped in a shared_ptr.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • I would follow your advice. As you say:"The in this code specifies that the generic T is specified in this case to be Object. It's not a good idea to think of it as if it was a temporary object. It's a specification.", one more question raises: **what "It's a specification."** mean? –  May 24 '20 at 06:52
  • @John it basically means that in terms of generics used here, `````` means "some type", which in general is used to make sure that you can reuse the same code in different use-cases. Here, in particular, you might want to create a shared_ptr of Object now, but later you may use a different type. At the time of the implementation of ```make_shared``` the people implementing it could not tell in advance that you will need a shared_ptr of Object. They were agnostic about it and decided to support any type. This is the basic idea behind generics. – Lajos Arpad May 24 '20 at 06:56
  • @John And here, the `````` part is specifying that ```T``` is ```Object``` in this case. It's nice that the funciton itself uses a generic type and supports any types, but, in your particular case the type is known and is to be specified. – Lajos Arpad May 24 '20 at 06:57
  • I see.Thank you for the clarification.I agree with @Remy Lebeau that `std::make_shared` is a template function.But Peter says that it is a helper function.What do you think of it? Thanks a lot . –  May 24 '20 at 07:02
  • @John it's both. It's a template function, because it's using the generic of `````` as a template. It's also a helper function because if performs part of the computation of your function. – Lajos Arpad May 24 '20 at 07:04