且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

可变参数模板的多重继承:如何为每个基类调用函数?

更新时间:2023-11-30 22:11:34

一种迭代可变参数基数的方法:

One way to iterate over the variadic bases:

template <typename T, typename ...Args>
class ChildGenerator : public Args...
{
public:
    ChildGenerator(T t) : Args(t)... {}

    void doThings() override {
        int dummy[] = {0, (Args::doThings(), void(), 0)...};
        static_cast<void>(dummy); // avoid warning for unused variable
    }
};

或在C ++ 17中,带有折叠表达式:

or in C++17, with folding expression:

    void doThings() override {
        (static_cast<void>(Args::doThings()), ...);
    }