Re: A template that calls a different function based on the object passed into the template
Ivan Novick wrote:
Ulrich Eckhardt wrote:
Since you give the template just a type, the '?' needs to be 'typename'.
Then, since there is no common implementation for different types, you
should just declare the function template and then provide specialisations
for the types you need.
The point is I need to call ZoomWhatever and Whatever could be any
number of thousands of different types which don't yet exist.
The language feature i am trying to get is a way to construct a
function name to call based upon the Type of the parameter.
template<typename T>
void do_it()
{
ZoomT();
}
If we can't figure out how to munge the string of the template
parameter with Zoom somehow, then how about some sort of traits
solution?
The real question is, What is the actual problem you're trying to solve?
However, if you're solidly wed to your current idea, here's a couple of
options:
1. Even though Zoom doesn't need any parameters, give it a dummy
parameter of type const T&, or (if T can be expensive), const T*, so
that you can overload.
E.g. instead of:
void ZoomThis();
void ZoomThat();
use
void Zoom(const This*);
void Zoom(const That*);
template<typename T>
void do_it()
{
Zoom(static_cast<const T*>(0));
}
2. Failing that, I guess you could use the preprocessor.
template<typename T> void do_it();
#define DO_IT(T) \
template<> \
void do_it<T>() \
{ \
Zoom##T(); \
}
But that's real ugly. If you can do it, use the overload instead.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]