Passing a function pointer to a templated class

Started by
7 comments, last by Structural 20 years, 6 months ago
Again a Functor question (seems to be a bit of a bandwaggon lately) How would I go about to passing a function with an arbitrary number of arguments to a templated class. I''ve tried the following:

template<class ClassType, typename Function>
class Functor
{
private:
	ClassType* obj;
	Function* func;

public:
	Functor(TClassType* o, Function f_)
	{
		obj = o; func = f_;
	}

};
However, how do I call this? I''ve tried: Functor f(myclassptr, myclassprt->function); I also tried: Functorfunction> f(myclassptr, myclassprt->function); And some other possibilities. I think I''ve had about any possible combination now. But I keep getting an error: myClass::function'' is invalid as template argument ''#3'', type expected What I want to do is pass any sort of member function to the Functor. Regardless of the arguments.
STOP THE PLANET!! I WANT TO GET OFF!!
Advertisement
This is all you need:

http://www.function-pointer.org/

You''re trying to get the member function pointer wrongly.
Can you give me a bit of a pointer (pun intended) on how to proceed? I can''t really find any information on arbitrary argument lists on the link you provided. It only explains how to call member functions with you prgramming the argument list into the functor, which I did before. I want to step off that static implementation and go completely flexible.
STOP THE PLANET!! I WANT TO GET OFF!!
You can''t support arbitary argument lists easily.

Use Boost.Function, they use a preprocessor to generate template code for 1-50 arguments.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Do you need the same functor template instance to receive several different types of functions with different amounts of parameters, or are you planning to make new template instantiation for each number of parameters the stored function needs to take?

Specifying the template type may be tiresome (and you''re doing it wrong, since you should specify the function parameters also), so use a templated function instead. C++ can automatically resolve the template parameters for templated functions:

template<class T>
void eater(T t) { }

eater(3); //note! no need to say eater<int>(3)
eater("aa"; //and no need to say eater<const char*>(3)
//the type of the templated function is deduced from the parameter

Then you can make a similar templated function whose purpose is to create Functor-objects. The standard library std::make_pair works similarly: it creates objects of type std:: pair and you don''t need to specify the template parameters.


If you want a ready-made solution for what it seems you''re trying to do, take a look at

http://www.boost.org/libs/bind/bind.html
and
http://www.boost.org/doc/html/function.html

antareus, My problem lies not with calling the function with the function list. I got that part going with overridden functions. The problem is getting the function pointer into my Functor.
At this moment I have to change the list in the functor statically:

Constructor:
ObjFunctor(TClassType* o, funcType f)

funcType is a typedef to a member function (I got this part from enginuity, kudos to the guy who wrote that)

typedef ReturnType (TClassType::*funcType)();
or if I want a function with an int as parameter:
typedef ReturnType (TClassType::*funcType)(int);

So, every time I call ObjFunctor f(obj, obj->function) I have to adjust the funcType in the functor to match the functions signature (that''s a crooked sentence, no?).

That''s the whole problem I''m facing. I want it to take any function pointer, regardless of its parameter list. Just the pointer... I don''t care about parameter lists there, I handle that part somewhere else.
Or am I missing out on somesort of memory reservation thing when I declare this function-pointer? Does it reserve some extra memory when you create a function-pointer?



AP, I know I''m trying to create a ready made solution like boost::Functor, but I took this up as a way to learn templates. Now I''m hooked on getting this to work properly as it''s been very interresting material so far.

I hope you lot can help me out with this. I know it''s very cryptic and my use of language might not be the clearest of all. But I really want to get this going.

Thanks!
STOP THE PLANET!! I WANT TO GET OFF!!
quote:However, how do I call this?

You have to specify the type of the class and function pointer:
Functor<MyClass, void (MyClass::*)()> f(myclassptr, &MyClass::function); 

quote:I want it to take any function pointer, regardless of its parameter list. Just the pointer... I don''t care about parameter lists there

Your original Functor is fine, with some modifications. If you use a function pointer type as a template argument, then Function is already a pointer type, so no *. To call the function, you need to overload operator() as many times as the number of arguments you wish to support:
template <class ClassType, typename Function>class Functor{    ClassType* object;    Function function;public:    Functor(ClassType* o, Function f) : object(o), function(f)    {    }    void operator()()    {        (object->*function)();    }        template <typename T>    void operator()(T t)    template <typename T1, typename T2>    void operator()(T1 t1, T2 t2)    template <typename T1, typename T2, typename T3>    void operator()(T1 t1, T2 t2, T3 t3)    // and so on...};


Hope that gave you some help.
I don''t see why the original poster is wasting his time with this when there are several functor implementations out there, including Boost and Loki''s.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Matsen, that was very helpfull!!! It works now. It appears that I did my last argument wrong all the time. Also what you said about leaving out the ''*'' character was something I did wrong.

Thanks again!

antareus, I''m not wasting any time here. This has been the first time I came into contact with templates, therefore I wanted to get this going to learn the basics about templates and some more about function pointers, and I must say it''s been a very instructive day for me today.

STOP THE PLANET!! I WANT TO GET OFF!!

This topic is closed to new replies.

Advertisement