Summit
Summit

Reputation: 2268

The syntax for smart pointer

In this declration for a unique pointer

  std::unique_ptr<SDL_Window, void(*)(SDL_Window*)> window_;

I could not find any resource with this kind of decration so my guess is that we create a pointer and pass it to the function and the return value is stored in window_

Upvotes: 1

Views: 70

Answers (2)

eerorika
eerorika

Reputation: 238331

I could not find any resource with this kind of decration

This is a variable declaration. Type of the variable is std::unique_ptr<SDL_Window, void(*)(SDL_Window*)> and name of the variable is window_. More specifically, the variable is default initialised, which in case of non-trivial types such as std::unique_ptr means that the default constructor will be called. The default constructor of std::unique_ptr initialises itself as null pointer.

Upvotes: 1

Useless
Useless

Reputation: 67733

No, this is just declaring the variable window_, it doesn't initialize it at all.

The resource you're looking for is here:

template<
  class T,
  class Deleter = std::default_delete<T>
  > class unique_ptr;

It's just declaring a unique ptr with a non-default deleter: the void(*)(SDL_Window*) part is the type of a function pointer, which means the correct deleter function must be passed in when you initialize window_.

If only one deleter function is really used, it's nicer to make it a functor, where the function is a static method of the functor type and doesn't need to be passed to the constructor.

Upvotes: 3

Related Questions