Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::shared_ptr: typedef with custom deleter [duplicate]

I am using SDL2 to develop a C++ application and want to use shared_ptr to hold the pointer stuff. So i do e.g. this:

typedef std::shared_ptr<SDL_Window> SDLWindowPtr;

and i need to use a custom deleter on this whole thing. Is it possible to include this within the typedef? If yes, how? The deleting function is called SDL_DestroyWindow. If not, how can i make the shared_ptr use SDL_DestroyWindow as custom deletion function?

Thanks in advance!

like image 381
Nidhoegger Avatar asked Mar 14 '23 21:03

Nidhoegger


1 Answers

Custom deleter is passed to shared_ptr in constructor, so it can't be done using typedef (deleter is not part of type of instantiated shared_ptr).

It could be done for unique_ptr (where deleter is part of type).

My suggestion: create factory method that will produce SDLWindowPtr (assigning them proper deleter). Something like std::make_shared but dedicated for your system (ex. createSDLWindow ?).

like image 57
Hcorg Avatar answered Mar 18 '23 06:03

Hcorg