I got some classes like
class A {
public:
void OnStageOne() {}
void OnStageTwo() {}
void OnStageThree() {}
};
class B {
public:
void OnStageOne() {}
void OnStageTwo() {}
void OnStageThree() {}
};
class C {
public:
void OnStageOne() {}
void OnStageTwo() {}
void OnStageThree() {}
};
and I got some vectors to store them:
std::vector<A> va;
std::vector<B> vb;
std::vector<C> vc;
now I need some operations like this: call OnStageXXX on all elements of a vector, so I wrote a template function like this:
template <typename T, void(T::*F)()>
void ForAll(std::vector<T> vector) {
for (auto& item : vector) {
(item.*F)();
}
}
It works fine, except that I have to use syntax like this:
ForAll<A, &A::OnStageOne>(va);
Since the first template parameter can be deduced for the parameter va, is there any way I can make the code more cleaner? Like ForAll<OnStageOne>(va);?
Or, if this is an A/B question, this there any way I can wrote things like ForAll<OnStageXXX>(va); to call OnStageXXX on each element of a vector?
P.S. I tried with std::for_each, but this makes the syntax even longer. I'm here to seek for a shorter syntax to do this, if any.