简体   繁体   English

类方法中的模板函数指针参数

[英]template function pointer parameter in a class method

I have a class method which takes in a function pointer of an object of any class but with a specific return and parameter list. 我有一个类方法,该方法接受任何类的对象的函数指针,但具有特定的返回和参数列表。
The problem is that the compiler returns that what I pass is not the same as the arguments of the method I'm using. 问题是编译器返回我传递的内容与我使用的方法的参数不同。
There are other things I'm have trouble with but I will point them out below and as always not every detail is here only whatever is important. 我还有其他麻烦的地方,但我会在下面指出,并且一如既往,并不是每个细节都只有重要的东西。

Class.h : Class.h

template<typename Value, typename ...Args>

class thing <Value(Args...)> 
{
    /* moar code */
    template<typename C> 
    void method(C* object, Value(C::*func)(Args...), Args... /*how do I pass all of the parameters from here*/) 
    {
        (object->*fund)(/*then put the parameters in here*/);
        /* Also would this work with or with out parameters being passed*/
        /* this is then wrapped and stored to be used later */
    }
}

In main func : 在主要功能中

thing<void(int)> thingObj;
AClass obj(/*initialise if needed*/);
thingObj.method(&obj, &AClass::func, /*Parameters*/);

Reason for not using boost/std::function is this is an experiment so I can lean how this works 不使用boost / std :: function的原因是这是一个实验,因此我可以了解其工作原理
Excuse the typos and lack of formatting on phone, will correct things and add detail wherever I feel necessary 请原谅电话上的错字和缺乏格式,在我认为有需要的地方,可以纠正问题并添加细节

You can use indices for it. 您可以为其使用索引。

template<std::size_t...> struct seq{};

template<std::size_t N, std::size_t... Is>
struct gen_seq : gen_seq<N-1, N-1, Is...>{};

template<std::size_t... Is>
struct gen_seq<0, Is...> : seq<Is...>{};

Then write overloading of your method like this 然后像这样编写方法的重载

template<typename C, size_t... indices>
void method(C* object, Value(C::*func)(Args...),
std::tuple<Args...>&& args, seq<indices...>)
{
   (object->*func)(std::get<indices>(args)...);
}

And call it in your first method like 然后用第一个方法调用它

method(object, func, std::forward_as_tuple(args...), gen_seq<sizeof...(Args)>{});

Live example 现场例子

This works for me: 这对我有用:

#include <iostream>

template <typename Signature>
class thing;

template <typename Value, typename... Args>
class thing <Value(Args...)> 
{
public:
    template<typename C> 
    void method(C* object, Value(C::*func)(Args...) const, Args&&... args) 
    {
        (object->*func)(std::forward<Args>(args)...);
    }
};

struct AClass
{
    void func(int i) const
    {
        std::cout << "passed " << i << std::endl;
    }
};

struct BClass
{
    void add(double a, double b) const
    {
        std::cout << a << " + " << b << " = " << a + b << std::endl;
    }
};

int main()
{
    thing<void(int)> thingObj;
    AClass obj;

    thingObj.method(&obj, &AClass::func, 5);

    BClass bobj;
    thing<void(double, double)> anotherThing;

    anotherThing.method(&bobj, &BClass::add, 10.2, 11);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM