是否存在"动态蜕变"?

She*_*ohn 6 c++ multiple-inheritance decltype c++11

这个问题decltype与多重继承有关.

假设我有以下内容:

  • 带有一些虚方法的抽象类A,
  • 一些使用前面的虚拟方法实现方法的派生类(这些类中的每一个都是一种用例),
  • 最后一个具体类,它继承了先前用例的一个子集,并实现了纯虚方法.

例如:

#include <iostream>

/**
 * "Iterable container"
 */
template <class T>
struct A
{
    virtual T* data() =0;
    virtual const T* data() const =0;
    virtual unsigned size() const =0;

    T* begin() { return data(); }
    T* end() { return data()+size(); }

    const T* begin() const { return data(); }
    const T* end() const { return data()+size(); }
};

// ------------------------------------------------------------------------

/**
 * Iterative assignment
 */
template <class T>
struct B: public A<T>
{
    auto operator =( const T& val ) -> decltype(*this)
    {
        for ( auto& v: *this ) v = val;
        return *this;
    }
};

/**
 * Iterative display
 */
template <class T>
struct C: public A<T>
{
    void show() const
    {
        for ( auto& v: *this )
            std::cout<< v << " ";
            std::cout<< std::endl;
    }
};

// ------------------------------------------------------------------------

/**
 * Concrete implementation
 */
template <class T, unsigned N>
struct D:
    public B<T>, 
    public C<T>
{
    using B<T>::operator=;

    T dat[N];

    T* data() { return dat; }
    const T* data() const { return dat; }
    unsigned size() const { return N; }
};

// ------------------------------------------------------------------------

int main()
{
    D<double,5> d;
    (d = 42).show(); // compile-time error, "no member named 'show' in 'B<double>'"
}
Run Code Online (Sandbox Code Playgroud)

问题是这个(没有双关语意); 如果其中一个"用例"方法应该返回一个引用*this,我想this成为对最终具体类的引用,这样我就可以使用其他用例中的其他方法来链接调用.

但是,在之前的实现中,我遇到了编译时错误.还有另一种方法来实现我解释的内容吗?

eca*_*mur 5

解决办法是使用CRTP;您告诉通过将最派生的类型作为附加模板参数传递B来返回左值引用D<T, N>

template <class T, class Derived>
struct B: public A<T>
{
    auto operator =( const T& val ) -> Derived&
    // ...

template <class T, unsigned N>
struct D:
    public B<T, D<T, N>>,
    // ...
Run Code Online (Sandbox Code Playgroud)

  • 不是,因为成员函数声明中的返回类型允许是不完整的类类型。当你实例化成员函数体时,`D&lt;T, N&gt;` 已经完成,所以一切正常。 (2认同)