获取模板可调用对象的参数类型

Gri*_*yan 17 c++ templates template-meta-programming c++11

考虑以下功能:

template<class F>
void register_handler( F& f ) // any callable object
{
   // find out T - the argument type of f
}
Run Code Online (Sandbox Code Playgroud)

f是一个可调用的对象,接受一个参数.它可以是函数指针,也可以std::function是结果std::bind.

问题是,如何确定参数类型f并根据该类型执行某些操作?


一个简单的解决方法是明确地将类型添加到模板中,例如

template<class T, class F> // T is the argument type of F
void register_handler( F& f )
Run Code Online (Sandbox Code Playgroud)

但这似乎有点过分,因为类型F应该已经包含有关类型的必要信息T.

lis*_*rus 15

假设F是任何可调用类型,则无法获取其参数类型.考虑一下:

struct callable
{
    void operator() (int);
    void operator() (float *);
    void operator() (std::string const &);
    void operator() (std::list<int> &);
};
Run Code Online (Sandbox Code Playgroud)

论证的类型在这里含糊不清.

  • @ galop1n:泛型lambdas只是`template <typename T> operator()`的糖,所以c ++ 11和c ++ 03中存在同样的问题. (4认同)
  • 如果您要事先定义可能的签名,则可以测试该类型是否可以使用这些签名进行调用,并忽略此"获取参数类型"无意义. (3认同)

Dav*_*rle 5

这篇博文显示了如何实现一些函数类型特征.这些应该适用于所有可调用的东西(例外:多态仿函数:P).您可以迭代参数,并使用它们的类型来做一些sfinae或作为额外的模板参数.

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

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

  • 我认为普遍的问题是我们不确切地知道类型参数的目的是什么。如果它用于执行 sfinae,则可以测试函子是否支持被调用。如果用于其他目的,则不能与多态函子一起使用。但用例可能是仅支持非多态函子。 (2认同)

ale*_*son 0

如果 F 是 a,std::function您应该能够使用它member type并使用 `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 .....

}
Run Code Online (Sandbox Code Playgroud)

这里有一个启动并运行的例子

但这种代码很快就会变得难以维护。