为什么将好奇模板模式的基 class 直接转换为另一个基 class 是错误的?

Why it is an error to convert a base class for curious template pattern to another base class directly?

我正在学习好奇的模板模式(crpt模式)。 我想用 crtp 模式减少 class 中的重复代码。 下面例子的要点是

  1. class C 是容器的维护者,class Container<(int)> 和 Container<(double)>

  2. class C提供了访问上面容器的方法

  3. 访问容器的功能是用crtp模式实现的GET_CONTAINRE_CRTP<(C)>.

以下代码运行良好。但是如果我改变它就不起作用 GET_CONTAINRE_CRTP::container_pair() 中案例 2 到案例 1 的代码;对我来说,case 1和case 2的代码是等价的。

为什么案例 1 的代码无法将 crtp 的基数 class 转换为另一个基数 class? 而且,我想弄清楚案例 2 的代码在 c++ 规则中是否有效,它提供了一种访问另一个基础的方法 class。

非常感谢。

代码:

template <typename T>
class Container : public std::vector<T> {};

template <typename Derived>
class GET_CONTAINRE_CRTP {
public:

    template <typename T>
    auto& container_pair(void) {

        // case 1: error at compiling
        // return static_cast<typename Derived::template ContianerChoice<T>&>(*this);

        // case 2: works well
        Derived* host = static_cast<Derived*>(this);
        return static_cast<typename Derived::template ContianerChoice<T>&>(*host);
    }
};

class C : private Container<int>, private Container<double>, public GET_CONTAINRE_CRTP<C> {
public:

    template <typename T>
    using ContianerChoice = Container<T>;

    C(void) {
        this->Container<int>::push_back(1);
        this->Container<double>::push_back(3);
    }

    friend class GET_CONTAINRE_CRTP<C>;

};


void test_get_container_by_crtp(void) {

    C c{};

    auto& container_int = c.container_pair<int>();
    std::cout << "value of contianer int at index 0 = " << container_int[0] << "." << std::endl;

    auto& container_double = c.container_pair<double>();
    std::cout << "value of contianer double at index 0 = " << container_double[0] << "." << std::endl;

}

上面test_get_container_by_crtp()的执行结果:

value of contianer int at index 0 = 1.
value of contianer double at index 0 = 3.

Why does code of case 1 fail to convert base class for crtp to another base class?

这就是 static_cast 的工作原理。 Container<To> 既不是派生的 class 也不是 GET_CONTAINRE_CRTP<C> 的基础 class,因此您不能使用 static_cast 在它们之间进行转换。
无论如何,恰好 Container<To>C 的基础 class,因此您可以使用 static_castGET_CONTAINRE_CRTP<C> 转换为其派生的 class Cthen 将其转换为所需的容器(即另一个子class)。
否则,您可以使用 reinterpret_cast 立即完成工作。

为了简化您的问题,您实际上是在问为什么以下内容不起作用:

struct base_a {};
struct base_b {};
struct derived : base_a, base_b {};

derived d;
base_a& a = d;
base_b& b = static_cast<base_b&>(a); // error: cannot cast from `base_a&` to `base_b&`

引用cppreference,给定表达式static_cast<new_type>(expression):

If new_type is a pointer or reference to some class D and the type of expression is a pointer or reference to its non-virtual base B, static_cast performs a downcast.

您正在尝试执行与 static_cast<base_b&>(a) 等效的操作,并且由于 base_a 不是 base_b 的非虚拟基础 class,因此这不是有效的沮丧。您需要向下转换为 derived& 然后隐式转换为 base_b&:

base_b& b = static_cast<derived&>(a);

或者你的情况:

template <typename T>
auto& container_pair(void) {
    using Choice = typename Derived::template ContianerChoice<T>;
    Choice& c = static_cast<Derived&>(*this);
    return c;
}