Daily bit(e) of C++ | Legacy resources with std::unique_ptr
Daily bit(e) of C++ ♻️85, Turning opaque pointers to resources into move-only RAII objects using std::unique_ptr.
A typical pattern with legacy (and C) APIs is for the initializing function to return an opaque pointer that needs to be freed by manually calling a cleanup function.
std::unique_ptr can serve as a helper here, as it accepts custom deleters. Unfortunately, the interface is not ergonomic, which we can fix by introducing a short helper.