Why?

Write and forgot it, 别再纠结这个free那个close了

smart_ptr.h

#pragma once
#include <memory>

template <typename T, auto Deleter>
using unique_ptr_dl = std::unique_ptr<std::remove_pointer_t<T>, decltype(Deleter)>;

template <typename T, auto Deleter>
unique_ptr_dl<T, Deleter> make_unique_ptr(T ptr) {
    return unique_ptr_dl<T, Deleter>(ptr);
}

template <typename T, auto Deleter>
using shared_ptr_dl = std::shared_ptr<std::remove_pointer_t<T>>;

template <typename T, auto Deleter>
shared_ptr_dl<T, Deleter> make_shared_ptr(T ptr) {
    return shared_ptr_dl<T, Deleter>(ptr, Deleter);
}

Examples

#include "smart_ptr.h"
#include <Windows.h>

auto pBuffer1 = make_unique_ptr<BYTE*, [](BYTE* p) { free(p); }>( static_cast<BYTE*>(malloc(1024)) );
auto pBuffer2 = make_shared_ptr<BYTE*, [](BYTE* p) { free(p); }>( static_cast<BYTE*>(malloc(1024)) );

auto pHandle  = make_unique_ptr<HANDLE, [](HANDLE p) { CloseHandle(p); }>( CreateFile(...) );
// And more...