-
Template functions
I'm making these classes, Expression_Set and Variable_Set into a template class called Set. I have a function that looks like this:
Code:
template<typename T> void Set::expand_factor_expressions() {
algo::for_each(members.begin(),members.end(),&T::expand_factor_expressions);
}
Here's my for_each function (declared in namespace algo):
Code:
template<typename _Iterator, typename _Function>
_Function for_each(_Iterator a, _Iterator z, _Function t) {
for (;a != z; ++a)
((*a).*t)();
return t;
}
I can't compile expand_factor_expressions because one of the arguments needs to contain the template. Is there a way around this? I could expand for_each, but I'd rather look for a more elegant solution.
-
That's a tough one. You may be able to dupe your compiler into making it work (but at the cost of uglying up your code a bit). Why not just use an old fassioned macro?
[edit]
Oops, I just realized that this is part of your template class. Stupid me.
is your for_each function static?
[/edit]
-
Can you pull expand_factor_expressions out of the classes and make them functors? You can then templatize them so you only need to say:
algo::for_each(members.begin(),members.end(),expan d_factor_expressions<T>());
-
I solved it by defining the functions inside the class definition. Not the best solution, but it works...
-
Why don't you explicitly give the call to for_each the template paramters (based off the type T of course).