问题描述
我有一个类方法,该方法采用任何类的对象的函数指针,但具有特定的返回和参数列表。
问题是编译器返回的是我传递的不是与我使用的方法的参数相同。
我还有其他麻烦,但是我会在下面指出,而且一如既往,并不是每个细节都重要。 / p>
Class.h :
template< ; typename值,类型名... Args>
类事物< Value(Args ...)>
{
/ *摩尔码* /
template< typename C>
void method(C * object,Value(C :: * func)(Args ...),Args ... / *如何从此处传递所有参数* /)
{
(object-> * fund)(/ *然后将参数放在这里* /);
/ *也可以在传递或不传递参数的情况下使用此功能* /
/ *然后将其包装并存储以供以后使用* /
}
}
在主函数中:
thing< void(int)> somethingObj;
AClass obj(/ *必要时初始化* /);
somethingObj.method(& obj,& AClass :: func,/ *参数* /);
不使用boost / std :: function的原因是这是一个实验,所以我可以学习一下作品
可以打扰打字错误和缺乏手机格式,可以在我认为有必要的地方更正内容并添加细节
这对我有用:
#include< iostream>
模板< typename签名>
类东西;
模板< typename值,typename ... Args>
类事物< 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<< 通过<<我<< std :: endl;
}
};
struct BClass
{
void add(double a,double b)const
{
std :: cout<< << +<< b<< =< a + b<< std :: endl;
}
};
int main()
{
something< void(int)> somethingObj;
AClass obj;
somethingObj.method(& obj,& AClass :: func,5);
BClass bobj;
something< void(double,double)>另一件事;
anotherThing.method(& bobj,& BClass :: add,10.2,11);
}
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:
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
Excuse the typos and lack of formatting on phone, will correct things and add detail wherever I feel necessary
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);
}
这篇关于类方法中的模板函数指针参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!