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

时间:2017-03-21 06:29:31

标签: c++ crtp

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

  1. C类是容器的维护者,类Container<(int)>和容器<(双重)>

  2. C类提供了一种访问上述容器的方法。

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

  4. 以下代码效果很好。但如果我改变它就行不通 GET_CONTAINRE_CRTP :: container_pair()中案例2到案例1的代码;对我来说,案例1和案例2的代码是等价的。

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

    非常感谢。

    代码:

    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.
    

2 个答案:

答案 0 :(得分:2)

为了解决你的问题,你基本上是在问为什么以下方法不起作用:

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)

  

如果new_type是某个类D的指针或引用,expression的类型是指向其非虚拟基础B的指针或引用,{{ 1}}执行向下投射

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

base_b&

或者在你的情况下:

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

答案 1 :(得分:1)

  

为什么案例1的代码无法将crtp的基类转换为另一个基类?

static_cast的工作原理。 Container<To>既不是派生类,也不是GET_CONTAINRE_CRTP<C>的基类,因此您无法使用static_cast在它们之间进行投射。
无论如何,Container<To>C的基类,因此您可以使用static_castGET_CONTAINRE_CRTP<C>投射到其派生类C然后将它投射到所需的容器(这是另一个子类) 否则,您可以使用reinterpret_cast一次完成工作。