Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to handle a variable number of parameters in a template class?

I have a set of callback classes that I use for handling callbacks with variable numbers of parameters. Right now I have about 6 different instances of it to handle differing numbers of arguments. Is there a way to make one instance than can handle a variable number of arguments?? Ultimately I would love to have each parameter be a POD type or a class pointer, or a struct pointer. Any ideas?

template <class T>
class kGUICallBackPtr
{
public:
    kGUICallBackPtr() {m_obj=0;m_func=0;}
    void Set(void *o,void (*f)(void *,T *));
    inline void Call(T *i) {if(m_func) m_func(m_obj,i);}
    inline bool IsValid(void) {return (m_func!=0);}
private:
    void *m_obj;
    void (*m_func)(void *,T *);
};


template <class T,class U>
class kGUICallBackPtrPtr
{
public:
    kGUICallBackPtrPtr() {m_obj=0;m_func=0;}
    void Set(void *o,void (*f)(void *,T *,U *));
    inline void Call(T *i, U *j) {if(m_func) m_func(m_obj,i,j);}
    inline bool IsValid(void) {return (m_func!=0);}
private:
    void *m_obj;
    void (*m_func)(void *,T *,U *j);
};
like image 546
KPexEA Avatar asked Sep 30 '08 20:09

KPexEA


People also ask

What is the role of parameter in a template?

A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

What is Variadic template in C++?

Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration.

What is a template parameter?

In UML models, template parameters are formal parameters that once bound to actual values, called template arguments, make templates usable model elements. You can use template parameters to create general definitions of particular types of template.

How do you use template arguments in C++?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)


1 Answers

Not yet in the language itself but C++0x will have support for variadic templates.

like image 186
jwfearn Avatar answered Sep 17 '22 06:09

jwfearn