如何获取C++ bind(...)表达式的签名

HC4*_*ica 11 c++ boost function signature c++11

我正在尝试编写一个名为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;
};
Run Code Online (Sandbox Code Playgroud)

它非常简单,大多数实际工作都是由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)
Run Code Online (Sandbox Code Playgroud)

对于lambdas:

auto f = [](int) { return 0; }
typedef signature_of<decltype(f)>::type Sig;  // Sig is int(int)
Run Code Online (Sandbox Code Playgroud)

对于仿函数:

struct A
{
    int operator()(int);
};
typedef signature_of<A>::type Sig;  // Sig is int(int)
Run Code Online (Sandbox Code Playgroud)

但是,它不适用于bind()表达式(这是仿函数的特例).如果我试试这个:

#include <functional>
int g(int);
typedef signature_of<decltype(std::bind(g, 0))>::type Sig;
Run Code Online (Sandbox Code Playgroud)

我收到编译器错误:

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
Run Code Online (Sandbox Code Playgroud)

问题是bind()返回的仿函数的operator()是一个模板,因此无法确定其类型.

是否有可能以另一种方式获取bind()表达式的签名?

Edw*_*nge 11

你遇到的问题多于绑定器的operator()是模板化的,它还有一个任意的参数计数.请记住,您应该能够使用任意数量的额外参数调用bind的结果.例如:

int f(int);

auto bound = boost::bind(f, _2);
Run Code Online (Sandbox Code Playgroud)

bound 现在可以使用任意数量的2或更多的参数调用,只有第二个实际转发到函数.

所以基本上,正如另一个答案所说,这个对象没有签名.它的签名仅由它的使用方式定义.