有没有办法让我的run函数调用参数包中包含的所有基类的 run 函数?使用标准的c ++工具/库还是我自己的模板元编程? C ++ 11更喜欢,但也对后来的标准感兴趣。
代码简化为最基本的示例。我最初有一个用例,但现在我只是感兴趣,是否可以提高我的理解。
template <class ...Bases> class Test : public Bases...
{
public:
void run()
{
// QUESTION: I want to call the run function of ALL the bases,
// Is there a way to do this that compiles and works?
Bases.run()...;
}
};
class One
{
public:
void run() {}
};
class Two
{
public:
void run() {}
};
int main()
{
Test<One, Two> test;
test.run();
}
答案 0 :(得分:3)
嗯,这只是通常的包装扩张。它们是你的基类这一事实并不会造成干扰。
C ++ 11:
int _[]{0, (void(Bases::run()), 0)...};
(void) _;
C ++ 17:
(void)(Bases::run(), ...);