Explore is it possible in c++17 to create uniqueptr with custom deleter using makeunique Save Share Download ▾ ReportPrivate research annotation Sign in to add a private research label or note.
is it possible in c++17 to create unique_ptr with custom deleter using make_unique
Assistant2025-05-15 11:05 UTC Copy response
No, in C++17, you cannot use std::make_unique to create a std::unique_ptr with a custom deleter directly. The std::make_unique function template always creates a std::unique_ptr with the default deleter (std::default_delete<T>).
Explanation
std::make_unique is designed to be a simple and efficient way to create a with the default deleter. The function signature roughly looks like this:
how to create scoped_ptr with custom deleter in c++17
Assistant2025-05-15 11:07 UTC Copy response
In C++17, there is no standard scoped_ptr type in the C++ Standard Library. scoped_ptr was a utility from the older Boost library, designed as a simple non-copyable smart pointer that deletes its object when it goes out of scope. Its closest standard counterpart is .
Copy first prompt Copy first response
Details
Provider openai
Model provenance As recorded by source dataset
Turns 2 (4 messages)
Language en
Added to archive Nov 24, 2026
ID 01m86m3c1qk4vkqkjmevmvt0xdSource & license Imported from WildChat-4.8M (source ). Imported records earn no Research Points.
Upstream record: facdf7269fb08fbeb8b87199a1053b90
allenai/WildChat-4.8M, revision c827c6df8fcf008219ffaffa4d1dd77491099367
License: ODC-BY-1.0
Cite this conversation “is it possible in c++17 to create uniqueptr with custom deleter using makeunique.” Conversation with gpt-4.1-mini-2025-04-14, from the WildChat-4.8M dataset. The Chat Archive, 2026. https://thechatarchive.com/c/01m86m3c1qk4vkqkjmevmvt0xd
std::unique_ptr<T>
cpp Copy code
template <typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args);
cpp Copy code
std::unique_ptr<T, std::default_delete<T>>
This means there is no way to specify a custom deleter type or instance via make_unique.
How to create a unique_ptr with a custom deleter? You have to construct the unique_ptr manually. For example:
cpp Copy code
#include <memory>
#include <iostream>
struct Foo {
Foo() { std::cout << "Foo constructor\n"; }
~Foo() { std::cout << "Foo destructor\n"; }
};
struct CustomDeleter {
void operator()(Foo* p) const {
std::cout << "Custom deleter called\n";
delete p;
}
};
int main() {
std::unique_ptr<Foo, CustomDeleter> p(new Foo, CustomDeleter{});
// use p as needed
}
If you want to avoid explicitly writing new, you can write a helper function of your own:
cpp Copy code
template<typename T, typename Deleter, typename... Args>
std::unique_ptr<T, Deleter> make_unique_with_deleter(Deleter d, Args&&... args) {
return std::unique_ptr<T, Deleter>(new T(std::forward<Args>(args)...), d);
}
int main() {
auto p = make_unique_with_deleter<Foo>(CustomDeleter{}, /* Foo constructor args */);
}
Summary
std::make_unique cannot be used with custom deleters in C++17.
Manually construct std::unique_ptr with new and the custom deleter.
Optionally, write your own make_unique_with_deleter helper.
This limitation remains in C++20 as well. (C++23 introduces std::make_unique_for_overwrite but still does not support custom deleters.)
std::unique_ptr
So, if you want a scoped RAII smart pointer with custom deleter in C++17, you would use std::unique_ptr with a custom deleter.
Creating a std::unique_ptr with a custom deleter in C++17 cpp Copy code
#include <memory>
#include <iostream>
struct Foo {
Foo() { std::cout << "Foo constructed\n"; }
~Foo() { std::cout << "Foo destructed\n"; }
};
struct CustomDeleter {
void operator()(Foo* p) const {
std::cout << "Custom deleter called\n";
delete p; // or any custom clean-up logic
}
};
int main() {
// Create unique_ptr with custom deleter
std::unique_ptr<Foo, CustomDeleter> p(new Foo, CustomDeleter{});
// Use p as normal
}
Why use std::unique_ptr instead of scoped_ptr?
scoped_ptr was never part of the C++ standard.
unique_ptr supports custom deleters (including lambda functions).
Both have exclusive ownership and delete the object when they go out of scope.
unique_ptr is moveable, while scoped_ptr is not, which generally improves flexibility.
Using a lambda as a custom deleter You can directly supply a lambda to unique_ptr:
cpp Copy code
auto deleter = [](Foo* p) {
std::cout << "Lambda deleter called\n";
delete p;
};
std::unique_ptr<Foo, decltype(deleter)> p(new Foo, deleter);
Note: You must explicitly specify the deleter type as a template parameter, e.g., decltype(deleter).
Why can't you use std::make_unique here? Because std::make_unique always creates std::unique_ptr<T, std::default_delete<T>> and does not accept a custom deleter .
In summary
Use std::unique_ptr<T, Deleter> to mimic scoped_ptr behavior with custom deleter.
Construct it with new T(...) and pass your deleter instance.
(Optional) Write a helper function if you want nicer syntax.
If you want me to, I can provide a helper function to simplify the creation of such unique_ptrs!
Dataset releases Not yet included in a published release.