I live nearby and drive past it nearly every day. It’s been fascinating watching its construction. They’ve done a very good job not impacting traffic and only closed down the highway late at night for a week or two.
namespace sdl2 {
using window = std::unique_ptr<SDL_Window, deleter<decltype(SDL_DestroyWindow), SDL_DestroyWindow>>;
}
...
sdl2::window window{SDL_CreateWindow(...)};
If you wanted to do something like what they have, I would create a variant on C++14's std::make_unique. That could look like: template<typename T> struct deleter_traits;
template<> struct deleter_traits<SDL_Window>
{
using custom_deleter = deleter<decltype(SDL_DestroyWindow), SDL_DestroyWindow>;
}
template<typename T, typename... Args> create_unique(Args&&... args)
{
return std::unique_ptr<T, typename deleter_traits<T>::custom_deleter>{new T(std::forward(args))};
}
sdl2::window window = create_unique<SDL_Window>(...);
That looks a lot cleaner to me, at least. And instead of having to write a new function for each type, you just add a new deleter_traits specialization. template<typename FuncType, FuncType& F> struct deleter { ... };
using unique_cptr = std::unique_ptr<void, deleter<decltype(free), free>>;
The other cool thing I learned recently is you can actually use the custom deleter to override the wrapped type by typedef'ing pointer in your deleter class. You can use this to wrap C-style file descriptors in a unique_ptr.