0

问题的主要部分是使用基于策略的设计可变参数模板的CRTP。从策略无法访问主/派生类的受保护或私有成员。由于使用可变参数模板,我不能将策略声明为朋友。

问题是,如何将所有策略类设置为派生类的朋友。

鉴于此 CRTP 解决方案,什么是支持多继承级别并解决了没有虚拟继承的菱形问题。

// Derived     - We would like to obtain access to this type of instance
// BaseDerived - Helper type to avoid the diamond problem without virtual inheritance
template<typename Derived, template<typename> class BaseDerived>
class Crtp {
protected:
    [[nodiscard]] constexpr Derived & underlying() noexcept
    {
        return static_cast<Derived &>(*this);
    }

    [[nodiscard]] constexpr Derived const & underlying() const noexcept
    {
        return static_cast<Derived const &>(*this);
    }
};

// Helper struct to achive multiple inheritance
struct NoDerivedClassTag;

template<template<typename> class Derived, typename Substitute, template<typename> class Base>
using DerivedCrtpBase = Base<std::conditional_t<std::is_same_v<Substitute, NoDerivedClassTag>, Derived<NoDerivedClassTag>, Substitute>>;

template<template<typename> class Interface, typename Object>
using is_crtp_interface_of = std::enable_if_t<
    std::is_same_v<Interface<NoDerivedClassTag>, Object> || std::is_base_of_v<Interface<typename Object::exact_type>, Object>>;

在带有可变参数模板的基于策略的设计中使用此 CRTP 解决方案

template<template<typename> class... Functionality>
class FinalDerived
    : public Functionality<FinalDerived<Functionality...>>...
{
public:
    constexpr int get() const
    {
        return protected_variable_;
    }

// Remove to check the problem
//protected:
    int protected_variable_ {-1};
};

目标是像这样使用策略中的受保护变量

template<typename Derived>
struct Increment
    : Crtp<Derived, Increment>
{
    void increment(int an_value)
    {
        this->underlying().protected_variable_ += an_value;
    }
};

template<typename Derived>
struct Decrement
    : Crtp<Derived, Decrement>
{
    void decrement(int an_value)
    {
        this->underlying().protected_variable_ -= an_value;
    }
};

使用示例

constexpr int number {7};

int main(void){
    FinalDerived<Increment, Decrement> derived;

    std::cout << "start: " << derived.get() << "\n";

    derived.increment(number);
    std::cout << "incremented: " << derived.get() << "\n";

    derived.decrement(number);
    std::cout << "decremented: " << derived.get() << "\n";
}

可运行示例

4

2 回答 2

0

我能想到的一个解决方案是使用特征类,由您在 CRTP 基础和策略之前创建和定义的类继承,因此它们将是完整的类。Trait 类可能包含指向您需要访问的成员的指针或引用,具有这些特征的类共享的类型声明等,看看它在标准组件中是如何完成的

于 2020-04-27T13:23:42.470 回答
0

正如Evg所写的,有一个带有正确答案的老问题。

#ifndef CRTP_VARIADIC_FRIEND

// M - Macro
// N - Number
// P - Packed parameters
// T - Type
#    define CRTP_FRIEND_REPEAT_2(M, N, P, T) M(N, P, T) M(N + 1, P, T)
#    define CRTP_FRIEND_REPEAT_4(M, N, P, T) CRTP_FRIEND_REPEAT_2(M, N, P, T) CRTP_FRIEND_REPEAT_2(M, N + 2, P, T)
#    define CRTP_FRIEND_REPEAT_8(M, N, P, T) CRTP_FRIEND_REPEAT_4(M, N, P, T) CRTP_FRIEND_REPEAT_4(M, N + 4, P, T)
#    define CRTP_FRIEND_REPEAT_16(M, N, P, T) CRTP_FRIEND_REPEAT_8(M, N, P, T) CRTP_FRIEND_REPEAT_8(M, N + 8, P, T)
#    define CRTP_FRIEND_REPEAT_32(M, N, P, T) CRTP_FRIEND_REPEAT_16(M, N, P, T) CRTP_FRIEND_REPEAT_16(M, N + 16, P, T)
#    define CRTP_FRIEND_REPEAT_64(M, N, P, T) CRTP_FRIEND_REPEAT_32(M, N, P, T) CRTP_FRIEND_REPEAT_32(M, N + 32, P, T)
#    define CRTP_FRIEND_REPEAT_128(M, N, P, T) CRTP_FRIEND_REPEAT_64(M, N, P, T) CRTP_FRIEND_REPEAT_64(M, N + 64, P, T)

#    define CRTP_FRIEND(N, P, T) \
        friend std::tuple_element_t<std::min(static_cast<std::size_t>(N + 1), sizeof...(P)), std::tuple<void, T...>>;

#    define CRTP_VARIADIC_FRIEND(P, T) CRTP_FRIEND_REPEAT_128(CRTP_FRIEND, 0, P, T)

#endif

我将尝试 Trait 类解决方案。稍后将是另一个答案。

可运行示例

于 2020-04-28T15:55:45.920 回答