-3

I have this line of code:

socket_LWIP = std::make_shared <framework::tcpip::socket::SocketLwip>();

where SocketLwip is a structure that has this constructor:

SocketLwip();

we defined under the : public section.


I understand that the discussed line of code creates a pointer to this structure, but it is initialized with a () which I don't quite understand. What does () do?

71GA
  • 1,132
  • 6
  • 36
  • 69
  • 1
    Its called *value initialization* and is explained in any beginner [c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason Dec 12 '22 at 12:35
  • See [dupe1](https://stackoverflow.com/questions/8860780/what-does-value-initializing-something-mean), [dupe2](https://stackoverflow.com/questions/29765961/default-value-and-zero-initialization-mess) and [dupe3](https://stackoverflow.com/questions/17131911/what-does-int-do-in-c). – Jason Dec 12 '22 at 12:37
  • 1
    I reopened this because it's not value initialization. – user253751 Dec 12 '22 at 13:20
  • 3
    it's something much more obvious. `()` calls a function. `std::make_shared` is the name of a function, and `()` calls it. – user253751 Dec 12 '22 at 13:20
  • 1
    And as it so happens, the implementation of `std::make_shared` forwards its parameters (which are `()` in this case) to the constructor. – j6t Dec 12 '22 at 13:30

1 Answers1

1

std::make_shared<T>() is a template function. The () is just its parameter list, like with any other function.

The code in question is simply calling std::make_shared<T>(), where T is being set to framework::tcpip::socket::SocketLwip.

std::make_shared<T>() creates a new instance of T, forwarding its own input parameters to T's constructor (which in this case, there aren't any), and returns that new instance wrapped inside of a std::shared_ptr<T> object.

That std::shared_ptr<T> object is then being assigned to the socket_LWIP variable.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thank you very much for answering without any judgement. As it turns out this is not as trivial as it seems at first. – 71GA Dec 12 '22 at 18:57