有"dynamic decltype"吗?

Is there a "dynamic decltype"?

本文关键字:dynamic decltype      更新时间:2023-10-16

这个问题与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>'"
}

问题是这样的(并非双关语);如果其中一个"用例"方法应该返回对*this的引用,我希望this是对最终具体类的引用,这样我就可以将调用与其他用例中的其他方法链接起来。

然而,在前面的实现中,我遇到了一个编译时错误。还有其他方法可以实现我所解释的吗?

解决方案是使用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>>,
    // ...

您可以给D一个重写operator=,返回一个D &:

auto operator =( const T& val ) -> decltype(*this) override
{
    B<T>::operator=(val);
    return *this;
}

这是因为D &B &是协变的,并且重写函数必须具有协变的返回类型。这当前也遮蔽了Boperator=,因为那个不是virtual