In some situations it\'s desirable to be able to type-erase a callable (e.g. function, function pointer, object instance with operator()
, lambda, mem_fn
Impossible. You may be able to take the address of operator()
for some types, but not for an arbitrary callable, because it may well have overloads or template parameters. Whether or not it would work for a lambda is most assuredly not well-defined, AFAIK.
For non-variadic non-generic captureless lambda functions as well as simple free functions one can use following approach:
#include <iostream>
#include <cstdlib>
template< typename L, typename R, typename ...A >
constexpr
auto // std::function< R (A...) >
to_function_pointer(L l, R (L::*)(A...) const)
{
return static_cast< R (*)(A...) >(l);
}
template< typename L, typename R, typename ...A >
constexpr
auto // std::function< R (A...) >
to_function_pointer(L l, R (L::*)(A...)) // for mutable lambda
{
return static_cast< R (*)(A...) >(l);
}
template< typename L >
constexpr
auto
to_function_pointer(L l)
{
return to_function_pointer(l, &L::operator ());
}
template< typename R, typename ...A >
constexpr
auto // std::function< R (A...) >
to_function_pointer(R (* fp)(A...))
{
return fp;
}
namespace
{
void f() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
}
int
main()
{
to_function_pointer([] () { std::cout << __PRETTY_FUNCTION__ << std::endl; })();
//to_function_pointer([&] () { std::cout << __PRETTY_FUNCTION__ << std::endl; })(); // can't cast from non-captureless lambda to function pointer
to_function_pointer([] () mutable { std::cout << __PRETTY_FUNCTION__ << std::endl; })();
to_function_pointer(f)();
to_function_pointer(&f)();
return EXIT_SUCCESS;
}
I've come up with a fairly nasty non-library solution, using the fact that lambdas have operator()
:
template<typename T> struct remove_class { };
template<typename C, typename R, typename... A>
struct remove_class<R(C::*)(A...)> { using type = R(A...); };
template<typename C, typename R, typename... A>
struct remove_class<R(C::*)(A...) const> { using type = R(A...); };
template<typename C, typename R, typename... A>
struct remove_class<R(C::*)(A...) volatile> { using type = R(A...); };
template<typename C, typename R, typename... A>
struct remove_class<R(C::*)(A...) const volatile> { using type = R(A...); };
template<typename T>
struct get_signature_impl { using type = typename remove_class<
decltype(&std::remove_reference<T>::type::operator())>::type; };
template<typename R, typename... A>
struct get_signature_impl<R(A...)> { using type = R(A...); };
template<typename R, typename... A>
struct get_signature_impl<R(&)(A...)> { using type = R(A...); };
template<typename R, typename... A>
struct get_signature_impl<R(*)(A...)> { using type = R(A...); };
template<typename T> using get_signature = typename get_signature_impl<T>::type;
template<typename F> using make_function_type = std::function<get_signature<F>>;
template<typename F> make_function_type<F> make_function(F &&f) {
return make_function_type<F>(std::forward<F>(f)); }
Any ideas where this can be simplified or improved? Any obvious bugs?