If you want to use a custom deleter with std::unique_ptr, you cannot use std::make_unique directly. Instead, you should use the constructor of std::unique_ptr directly.
Example: Using std::unique_ptr with a custom deleter
#include <iostream>
#include <memory>
void customDeleter(int* ptr) {
std::cout << "Deleting int with custom deleter: " << *ptr << std::endl;
delete ptr;
}
int main() {
// Create using `new` and use unique_ptr constructor
std::unique_ptr<int, decltype(&customDeleter)> uptr(new int(42), customDeleter);
return 0; // customDeleter will be called
}
If You Want to Emulate make_unique with a Deleter
You could write a helper function similar to make_unique, but supporting a custom deleter: