I am looking for a type that converts a free function to a default function construct object.
It should be a class template that takes a function as a template parameter:
template<typename F, F P> struct fn_to_type { template<class... Args> decltype(auto) operator()(Args&&... args) { return P(std::forward<Args>(args)...); } };
Therefore, it can be used as a template parameter for containers and smart pointers:
bool CloseHandle(void* handle); using UniqueHandle = std::unique_ptr<void, fn_to_type<decltype(&CloseHandle), &CloseHandle>>; bool FooLess(const Foo& lhs, const Foo& rhs); using FooSet = std::set<Foo, fn_to_type<decltype(&FooLess), &FooLess>>;
The standard library definitely does not have such a function, but perhaps the Boost library?
I'm also curious why such a thing is missing from the standard library - are there any pitfalls that I don't see?
And if there is no library with such a function, is there a way to improve this fn_to_type thing? For instance. do something so as not to print the function name twice?
Abyx source share