T
tygro
Hi,
I would like to do something like this:
class Type
{
public:
template <template<typename> class Fun>
void applyOnType(Fun f, void* data)
{
switch(type) {
case 0:
f<int>(data);
break;
case 1:
f<short>(data);
break;
}
};
Then I could call:
template <typename T> void myFunction(T* data) { }
DataType dt;
dt.applyOnType(myFunction, data);
But it doesn't work because I guess it is not possible to pass a
template function in argument to a function.
Furthermore, because of the template-template, it seems that Fun must be
functor.
Do you see a way to write something as flexible as this but which could
compile?
My only working solution was this:
class TypeFunc
{
public:
template<typename T> void operator()() {}
};
void onDataType(TypeFunc& f, void* data)
{
switch(..) {
...
f.operator()<int>();
...
}
}
I don't think it's easy to use. I also have a macro based solution, but
I would prefer something 100% template.
Thank you for your help.
I would like to do something like this:
class Type
{
public:
template <template<typename> class Fun>
void applyOnType(Fun f, void* data)
{
switch(type) {
case 0:
f<int>(data);
break;
case 1:
f<short>(data);
break;
}
};
Then I could call:
template <typename T> void myFunction(T* data) { }
DataType dt;
dt.applyOnType(myFunction, data);
But it doesn't work because I guess it is not possible to pass a
template function in argument to a function.
Furthermore, because of the template-template, it seems that Fun must be
functor.
Do you see a way to write something as flexible as this but which could
compile?
My only working solution was this:
class TypeFunc
{
public:
template<typename T> void operator()() {}
};
void onDataType(TypeFunc& f, void* data)
{
switch(..) {
...
f.operator()<int>();
...
}
}
I don't think it's easy to use. I also have a macro based solution, but
I would prefer something 100% template.
Thank you for your help.