I am looking for a standard template like C ++ 14, which statically (at compile time) introduces a link to a function as an argument to a template and implements it operator()as a call forwarding function.
I know that std::functionexists, but it stores a pointer to a function as a data element. I want the function reference to be embedded in the type signature so that the wrapper type is empty and default construct.
I have a working implementation (with an example use):
#include <cstring>
#include <iostream>
#include <memory>
template <typename Ret, typename... Args>
struct fn_t {
template <Ret (Func)(Args...)>
struct fn_ref {
Ret operator () (Args &&...args) const {
return Func(std::forward<Args>(args)...);
}
};
};
template <typename T>
using unique_c_ptr = std::unique_ptr<T, fn_t<void, void *>::fn_ref<std::free>>;
int main() {
std::unique_ptr<char[], decltype(&std::free)> ptr1(::strdup("Hello"), &std::free);
unique_c_ptr<char[]> ptr2(::strdup("Hello"));
std::cout << sizeof ptr1 << '\n'
<< sizeof ptr2 << std::endl;
return 0;
}
ptr1and ptr2work the same, but ptr2half the size, because it does not need to keep a pointer to std::free.
: , fn_t fn_ref?