简体   繁体   English

如何在可变参数模板类中获取函数指针的参数类型?

[英]How do I get the argument types of a function pointer in a variadic template class?

This is a follow up of this problem: Generic functor for functions with any argument list这是这个问题的后续: Generic functor for functions with any argument list

I have this functor class (full code see link above):我有这个函子类(完整代码见上面的链接):

template<typename... ARGS>
class Foo
{
    std::function<void(ARGS...)> m_f;
public:
    Foo(std::function<void(ARGS...)> f) : m_f(f) {}
    void operator()(ARGS... args) const { m_f(args...); }
};

In operator() I can access the args... easily with a recursive "peeling" function as described in Stroustrup's C++11 FAQoperator()我可以使用Stroustrup 的 C++11 FAQ 中描述的递归“剥离”函数轻松访问args...

My problem is: I want to access the types of the arguments of f, ie ARGS... , in the constructor.我的问题是:我想在构造函数中访问 f 的参数类型,即ARGS... Obviously I can't access values because there are none so far, but the argument type list is somehow burried in f , isn't it?显然我无法访问值,因为到目前为止还没有,但是参数类型列表以某种方式隐藏在f ,不是吗?

You can write function_traits class as shown below, to discover the argument types, return type, and number of arguments:您可以编写如下所示的function_traits类,以发现参数类型、返回类型和参数数量:

template<typename T> 
struct function_traits;  

template<typename R, typename ...Args> 
struct function_traits<std::function<R(Args...)>>
{
    static const size_t nargs = sizeof...(Args);

    typedef R result_type;

    template <size_t i>
    struct arg
    {
        typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
    };
};

Test code:测试代码:

struct R{};
struct A{};
struct B{};

int main()
{
   typedef std::function<R(A,B)> fun;

   std::cout << std::is_same<R, function_traits<fun>::result_type>::value << std::endl;
   std::cout << std::is_same<A, function_traits<fun>::arg<0>::type>::value << std::endl;
   std::cout << std::is_same<B, function_traits<fun>::arg<1>::type>::value << std::endl;
} 

Demo : http://ideone.com/YeN29演示: http : //ideone.com/YeN29

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用变量模板类方法将函数指针作为参数与从函数模板派生的类型? - How to make variadic template class method take function pointer as argument with type derived from function template? 如何在没有 arguments 的 function 中迭代/获取 static 成员? - How do I iterate over/get static members from the variadic template types in a function with no arguments? 获取std :: function可变参数模板参数类型的自定义sizeof - Get custom sizeof of std::function's variadic template argument types 如何使此模板参数可变参数? - How do I make this template argument variadic? 如何使用函数指针作为非类型参数制作可变参数模板函数? - how can I make variadic template function with a function pointer as non type argument? 如何定义采用可变参数 class 模板的 function? - How do I define a function that takes a variadic class template? 如何将可变参数模板参数转换为另一种类型以调用另一个函数? - How to transform a variadic template argument to another types for calling another function? 如何将元函数应用于可变参数模板类的模板类型? - How to apply a meta function to the template types of a variadic template class? 不同参数类型的可变参数模板函数中的递归 - recursion in variadic template function of different argument types 具有相等参数类型的可变参数模板函数 - Variadic template function with equal argument types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM