我正在尝试编写一个名为signature_of的元函数,给定函数(指针),仿函数或lambda的类型,返回其签名。
这是我到目前为止所拥有的:
#include <boost/mpl/pop_front.hpp>
#include <boost/mpl/push_front.hpp>
#include <boost/function_types/is_member_function_pointer.hpp>
#include <boost/function_types/function_type.hpp>
#include <boost/function_types/result_type.hpp>
#include <boost/function_types/parameter_types.hpp>
#include <type_traits>
template <typename F>
struct signature_of_member
{
typedef typename boost::function_types::result_type<F>::type result_type;
typedef typename boost::function_types::parameter_types<F>::type parameter_types;
typedef typename boost::mpl::pop_front<parameter_types>::type base;
typedef typename boost::mpl::push_front<base, result_type>::type L;
typedef typename boost::function_types::function_type<L>::type type;
};
template <typename F, bool is_class>
struct signature_of_impl
{
typedef typename boost::function_types::function_type<F>::type type;
};
template <typename F>
struct signature_of_impl<F, true>
{
typedef typename signature_of_member<decltype(&F::operator())>::type type;
};
template <typename F>
struct signature_of
{
typedef typename signature_of_impl<F, std::is_class<F>::value>::type type;
};
它非常简单,大多数实际工作都是由boost :: function_types库完成的。 一般的想法是:
- 使用std :: is_class来区别对待 内置函数之间(包括 lambdas)和算子
- 对于内置函数类型,使用boost :: function_types :: function_type来获取其签名
- 对于仿函数,获取其运算符的类型(),获取其签名,并通过医生来删除“this”参数
这适用于内置函数:
int f(int);
typedef signature_of<decltype(f)>::type Sig; // Sig is int(int)
对于lambdas:
auto f = [](int) { return 0; }
typedef signature_of<decltype(f)>::type Sig; // Sig is int(int)
对于仿函数:
struct A
{
int operator()(int);
};
typedef signature_of<A>::type Sig; // Sig is int(int)
但是,它不适用于bind()表达式(这是仿函数的特例)。如果我试试这个:
#include <functional>
int g(int);
typedef signature_of<decltype(std::bind(g, 0))>::type Sig;
我收到编译器错误:
In file included from test.cpp:3:0:
signature_of.hpp: In instantiation of 'signature_of_impl<
_Bind<int (*(int))(int)>, true
>':
signature_of.hpp:45:74: instantiated from 'signature_of<
_Bind<int (*(int))(int)>
>'
test.cpp:21:52: instantiated from here
signature_of.hpp:39:74: error: type of '& _Bind<
int (*)(int)({int} ...)
>::operator()' is unknown
问题是bind()返回的仿函数的operator()是一个模板,因此无法确定其类型。
是否有可能以另一种方式获取bind()表达式的签名?