我有这段代码:
#include <iostream>
template<class P>
void processAll() {
P p = P();
p.process();
}
class P1 {
public:
void process() { std::cout << "process1" << std::endl; }
};
int main() {
processAll<P1>();
return 0;
}
有没有一种使用模板可变参数的方法,可以将第二个类'P2'注入到我的函数'processAll'中?就像这样:
...
template<class... Ps>
void processAll() {
// for each class, instantiate the class and execute the process method
}
...
class P2 {
public:
void process() { std::cout << "process2" << std::endl; }
};
...
int main() {
processAll<P1, P2>();
return 0;
}
我们可以迭代每个类吗?