在 C++ 中迭代 class 继承

Iterate over class inheritances in C++

假设我有一些 classes 架构(classes 的数量在开发期间不断增加),每个 class 继承自 N classes 具有相同的基本界面。创建将迭代继承的基函数(在基 class 或派生 class 中)的最佳方法(如果可能)是什么?

目标: 避免开发人员犯错误并确保我们不会忘记调用所有继承的所有基函数并使代码更清晰易读。

请查看更新状态的编辑说明

短例:

class shared_base {
public:
    virtual void func() = 0;
}

class base_1 : virtual public shared_base {
public:
    void func() override {}
}

class base_2 : virtual public shared_base {
public:
    void func() override {}
}

class target : virtual public base_1, virtual public base_2 {
public:
    void func() override {
        // Instead of:
        base_1::func();
        base_2::func();
        // ... My func() implementation
        /*
        ~~TODO~~
        for_each(std::begin(inheritances), std::end(inheritances), [](auto& inheritance) -> void { inheritance::func(); })
        ~~TODO~~
        */
    }
}

更具描述性和实用性的示例:

class base {
public:
    virtual void func() = 0;
    /*...Some interface (pure virtual) functions...*/
}

class base_core : virtual public base {
public:
    void func() override {}
    /*...Some base implementations for the rest...*/

protected:
    template <typename FuncT>
    virtual void iterate_over_base_core_inheritances(FuncT function_to_apply) {
        /*~~TODO~~*/
    }
}

template <class Decorator = base_core, typename = typename std::enable_if<std::is_base_of<base_core, Decorator>::value>::type>
class core_1 : virtual public Decorator {
public:
    void func() override {
        // Will iterate (once) over Decorator
        /*iterate_over_base_core_inheritances([](core_base*) -> void {
            // Implementation
        });*/
        // Instead of:
        Decorator::func();
    }
    /*More functions implementations*/
}

template <class Decorator = base_core, typename = typename std::enable_if<std::is_base_of<base_core, Decorator>::value>::type>
class core_2 : virtual public core_1<>, virtual public Decorator {
public:
    void func() override {
        // Will iterate (twice) over core_1 and Decorator
        /*iterate_over_base_core_inheritances([](core_base*) -> void {
            // Implementation
        });*/
        // Instead of:
        Decorator::func();
        core_1::func();
        //... Self func() implementation
    }
    /*More functions implementations*/

protected:
    // If it's not possible doing it in the upper hierarchy level is it possible do it here?
    template <typename FuncT>
    void iterate_over_base_core_inheritances(FuncT function_to_apply) override {
        /*~~TODO~~*/
    }
}

需要知道的一些事情:

一个实际例子(我实际使用的)可以在this commit中找到。

编辑:

Thanks to @RaymondChen 我得到了一个可行的解决方案,(到目前为止)只有一个小问题:每次我想使用 class 实现这样,我需要在它的模板参数列表中指定 core_base class(在我使用默认类型参数之前)。我正在寻找解决此问题的方法。
当前解法:

template <class ...Decorators>
class core_2 : virtual public Decorators... {
public:
    static_assert((std::is_base_of<base_core, Decorators>::value && ...), "All decorators must inherit from base_core class.");

    void func() override {
        (Decorators::func(), ...);
        //... Self func() implementation
    }
    /*More functions implementations*/
}

创建实例示例:
当前:
std::shared_ptr<base> base = std::make_shared<core_2<core_1<base_core>, core_3<base_core>>>();
期望:
std::shared_ptr<base> base = std::make_shared<core_2<core_1<>, core_3<>>>();

this commit.

中可以找到实际示例(供我实际使用)

多亏了 @RaymondChen 我使用以下解决方案非常接近我的原始目标 [请参阅底部的更新部分]:

template <class ...Decorators>
class core_2 : virtual public Decorators... {
public:
    static_assert((std::is_base_of<base_core, Decorators>::value && ...), "All decorators must inherit from base_core class.");

    void func() override {
        (Decorators::func(), ...);
        //... Self func() implementation
    }
    /*More functions implementations*/
}

解释:

使用参数包我们可以创建一个 "list" 的 class 我们继承自,使用折叠表达式 [c++17] 我们可以在几行代码中实现它。

优点与我最初的想法相比:

  • 对象创建线现在更加清晰和合乎逻辑:
    之前:
    std::shared_ptr<base> base = std::make_shared<core_2<core_1<core_3<>>>>();
    之后:
    std::shared_ptr<base> base = std::make_shared<core_2<core_1<base_core>, core_3<base_core>>>();
    因为 core_1 和 core_3 是独立的,但是 core_2 正在使用它们。
  • base/derived class 中不需要新函数,它刚好适合目标行(例如 is_equal 中未提及的函数 post).

丢失的功能:

  • is_base_of 的模板验证(用 static_assert & fold expressions 解决)。
  • 默认继承,以防无法指定任何继承(仍在尝试解决)。
    当前:
    std::shared_ptr<base> base = std::make_shared<core_2<core_1<base_core>, core_3<base_core>>>();
    期望:
    std::shared_ptr<base> base = std::make_shared<core_2<core_1<>, core_3<>>>();

更新

经过大量研究和尝试,我提出了以下解决方案(还使用 C++20 concepts 功能进行了改进):

template <class T>
        concept Decorator = std::is_base_of_v<base_core, T>;

class empty_inheritance {};

template<typename Base = base_core, typename ...Decorators>
struct base_if_not_exists {
    static constexpr bool value = sizeof...(Decorators);
    using type = typename std::conditional<value, empty_inheritance, Base>::type;
};

template <Decorator ...Decorators>
class core_2 : virtual public base_if_not_exists<base_core, Decorators...>::type, virtual public Decorators... {
public:
    void func() override {
        if constexpr (!base_if_not_exists<base_core, Decorators...>::value) {
            base_core::func();
        }
        (Decorators::func(), ...);
        //... Self func() implementation
    }
    /*More functions implementations*/
}

没有功能丢失:)