Consider the following function:
template<class F>
void register_handler( F& f ) // any callable object
{
// find out T - the argument type of f
}
Here f
is some callable object, accepting one argument. It may be a function pointer, an std::function
or a result of std::bind
.
The problem is, how to determine the argument type of f
and do some actions based on that type?
An easy workaround would be to add the type to template explicitly, like
template<class T, class F> // T is the argument type of F
void register_handler( F& f )
but this seems an overkill because type F
should already contain the necessary information about type T
.
Assuming F
is any callable type, you cannot get its argument type. Consider this:
struct callable
{
void operator() (int);
void operator() (float *);
void operator() (std::string const &);
void operator() (std::list<int> &);
};
the type of argument is an ambiguity here.
This blogpost shows how to implement some function type traits. These should work with everything callable (exception: polymorphic functors :P). You could iterate over the arguments, and use their type to do some sfinae or as a additional template argument.
Function traits as copied from blogpost:
#include <tuple>
// as seen on http://functionalcpp.wordpress.com/2013/08/05/function-traits/
template<class F>
struct function_traits;
// function pointer
template<class R, class... Args>
struct function_traits<R(*)(Args...)> : public function_traits<R(Args...)>
{};
template<class R, class... Args>
struct function_traits<R(Args...)>
{
using return_type = R;
static constexpr std::size_t arity = sizeof...(Args);
template <std::size_t N>
struct argument
{
static_assert(N < arity, "error: invalid parameter index.");
using type = typename std::tuple_element<N,std::tuple<Args...>>::type;
};
};
// member function pointer
template<class C, class R, class... Args>
struct function_traits<R(C::*)(Args...)> : public function_traits<R(C&,Args...)>
{};
// const member function pointer
template<class C, class R, class... Args>
struct function_traits<R(C::*)(Args...) const> : public function_traits<R(C&,Args...)>
{};
// member object pointer
template<class C, class R>
struct function_traits<R(C::*)> : public function_traits<R(C&)>
{};
// functor
template<class F>
struct function_traits
{
private:
using call_type = function_traits<decltype(&F::operator())>;
public:
using return_type = typename call_type::return_type;
static constexpr std::size_t arity = call_type::arity - 1;
template <std::size_t N>
struct argument
{
static_assert(N < arity, "error: invalid parameter index.");
using type = typename call_type::template argument<N+1>::type;
};
};
template<class F>
struct function_traits<F&> : public function_traits<F>
{};
template<class F>
struct function_traits<F&&> : public function_traits<F>
{};
Testcode:
#include <iostream>
class A
{
};
template <class T>
struct Functor
{
void operator()(const T& t)
{}
};
struct Register
{
//int parameters
template <class T>
static void RegisterFunctor(const T& /*functor*/, typename std::enable_if<std::is_same<typename function_traits<T>::template argument<0>::type, const int&>::value>::type* = 0)
{
std::cout << "Register int func" << std::endl;
}
//A parameters
template <class T>
static void RegisterFunctor(const T& /*functor*/, typename std::enable_if<std::is_same<typename function_traits<T>::template argument<0>::type, const A&>::value>::type* = 0)
{
std::cout << "Register int func" << std::endl;
}
};
void intFunc(const int&) {}
void aFunc(const A&){}
int main(int /*argc*/, char */*argv*/[])
{
Functor<int> intFunctor;
Functor<A> aFunctor;
Register::RegisterFunctor(intFunctor);
Register::RegisterFunctor(&intFunc);
Register::RegisterFunctor(aFunctor);
Register::RegisterFunctor(&aFunc);
return 0;
}
if F is a std::function
you should be able to use the its member type
and check with `std::is_same':
template<class F>
void register_handler( F& f ) // any callable object
{
// find out T - the argument type of f
if(std::is_same<int, F::argument_type>::value)
{ .... }
//etc .....
}
An up and running example here
but that kind of code can quickly become a mess to maintain.
You could use sfinae and test if your argument is convertible to a std::function with the required arguments:
#include <type_traits>
#include <functional>
#include <iostream>
class A
{
};
template <class T>
struct Functor
{
void operator()(const T& t)
{}
};
struct Register
{
//int parameters
template <class T>
static void RegisterFunctor(const T& /*functor*/, typename std::enable_if<std::is_constructible<typename std::function<void (int)>, T>::value >::type* = 0)
{
std::cout << "Register int func" << std::endl;
}
//A parameters
template <class T>
static void RegisterFunctor(const T& /*functor*/, typename std::enable_if<std::is_constructible<typename std::function<void (A)>, T>::value >::type* = 0)
{
std::cout << "Register a func" << std::endl;
}
};
void intFunc(int) {}
void aFunc(A){}
int main(int /*argc*/, char */*argv*/[])
{
Functor<int> intFunctor;
Functor<A> aFunctor;
Register::RegisterFunctor(intFunctor);
Register::RegisterFunctor(&intFunc);
Register::RegisterFunctor(aFunctor);
Register::RegisterFunctor(&aFunc);
return 0;
}
In that case, you can use a very simple library Boost.Callable Traits.
Example of using it :
#include <boost/callable_traits.hpp>
#include <iostream>
#include <tuple>
template<typename F>
void register_handler(F&)
{
if constexpr (std::is_same_v<boost::callable_traits::function_type_t<F>, void(int&, double)>)
{
std::cout << "Register handler with syntax void(int&, double)" << std::endl;
}
else if constexpr (std::is_same_v<boost::callable_traits::function_type_t<F>, void(int)>)
{
std::cout << "Register handler with syntax void(int)" << std::endl;
}
}
void func(int&, double)
{}
auto lambda = [](int) {};
int main()
{
{
register_handler(func);
register_handler(lambda);
}
{
using function_type = boost::callable_traits::function_type_t<decltype(func)>;
using expected_function_type = void(int&, double);
std::cout << std::boolalpha << std::is_same_v<expected_function_type, function_type> << std::endl;
}
}
To get type of function you can use boost::callable_traits::function_type_t<decltype(func)>
.
As you can see in main
and register_handler
functions, it's possible to compare expected_function_type
type with function type (boost::callable_traits::function_type_t<FUNCTION>
) using std::is_same_v
"function" -> https://en.cppreference.com/w/cpp/types/is_same
If you want to run my example, please compile it with boost 1.66.0 and c++17 using e.g gcc 7.1.0. Here you can do it online :)
来源:https://stackoverflow.com/questions/22630832/get-argument-type-of-template-callable-object