PAYATHON 2026

CRTP 中如何定义 copy assignment operator 避免 ill-formed?

数据小陈

这里有两个独立问题。第一,Vector<T,3> 里的 T& x = this->values[0]; 是引用数据成员。引用成员不能被重新绑定,所以编译器会把该类的隐式 copy assignment 和 move assignment 都定义为 deleted。你在 CRTP 基类里写的 operator=(const D&) 只是基类成员,不是派生类 D 的 copy assignment,替代不了派生类被删除的赋值运算符。第二,CRTP 基类成员函数中的 *this 静态类型是 VectorBase<...>,不是派生类 D,因此 return *this; 不能直接绑定到 D&。这里需要写成 static_cast<D&>(*this)。

为什么会出现这些错误

  • 对 foo = bar;,编译器会先找 Vector<float,3> 自己的 copy assignment。类里有引用成员 x,隐式 copy assignment 被删除,所以这里直接报错。基类的 operator=(const D&) 不会成为派生类的 copy assignment。
  • 对 bar = Vector<float,3>{...};,临时对象需要 move assignment 或 copy assignment;引用成员同样会导致隐式 move assignment 被删除。
  • 对 foo += bar;,调用的是基类 operator+=。函数体内 this 的类型是 VectorBase<T,3,Vector<T,3>>*,*this 是基类左值,不能隐式转成 Vector<float,3>&,所以 return *this; 报错。正确写法是 return static_cast<D&>(*this);。
  • 原示例里的 delete[] values; values = new T[N]; 也有问题:values 是 T values[N] 数组,不是动态分配的指针,不能 delete[],也不能把 new T[N] 赋给数组。复制数组应逐元素复制,或者使用 std::copy。另外,operator+ 里应 return lhs;,不是 return rhs;。

修复方式一:保留引用成员,显式定义派生类的特殊成员函数

如果确实要保留 T& x 这样的引用成员,就必须在 Vector<T,3> 里自己写 copy/move assignment。copy/move constructor 也建议显式写出来,否则默认 copy constructor 会让新对象的 x 绑定到源对象的 values[0],而不是它自己的 values[0]。赋值时不要给 x 赋值,只复制 values,因为 x 已经绑定到当前对象的 values[0]。

#include <algorithm>
#include <cstddef>
#include <utility>

namespace Details {
template<typename T, std::size_t N, class D>
class VectorBase {
public:
    T values[N]{};

    D& operator=(const D& rhs) {
        if (this != static_cast<const VectorBase*>(&rhs)) {
            std::copy(rhs.values, rhs.values + N, values);
        }
        return static_cast<D&>(*this);
    }

    D& operator+=(const D& rhs) {
        for (std::size_t i = 0; i < N; ++i) {
            values[i] += rhs.values[i];
        }
        return static_cast<D&>(*this);
    }

    friend D operator+(D lhs, const D& rhs) {
        lhs += rhs;
        return lhs;
    }
};
}

template<typename T, std::size_t N>
class Vector : public Details::VectorBase<T, N, Vector<T, N>> {};

template<typename T>
class Vector<T, 3> : public Details::VectorBase<T, 3, Vector<T, 3>> {
public:
    T& x = this->values[0];

    Vector() = default;

    Vector(T a, T b, T c) {
        this->values[0] = a;
        this->values[1] = b;
        this->values[2] = c;
    }

    Vector(const Vector& rhs) {
        for (std::size_t i = 0; i < 3; ++i) {
            this->values[i] = rhs.values[i];
        }
    }

    Vector(Vector&& rhs) noexcept {
        for (std::size_t i = 0; i < 3; ++i) {
            this->values[i] = std::move(rhs.values[i]);
        }
    }

    Vector& operator=(const Vector& rhs) {
        if (this != &rhs) {
            for (std::size_t i = 0; i < 3; ++i) {
                this->values[i] = rhs.values[i];
            }
        }
        return *this;
    }

    Vector& operator=(Vector&& rhs) noexcept {
        if (this != &rhs) {
            for (std::size_t i = 0; i < 3; ++i) {
                this->values[i] = std::move(rhs.values[i]);
            }
        }
        return *this;
    }
};

此时 foo = bar; 会调用显式 copy assignment,bar = Vector<float,3>{...}; 可以调用显式 move assignment,foo += bar; 也能通过 static_cast<D&>(*this) 正常返回。

修复方式二:把引用数据成员改成访问器函数

如果不需要在对象布局里保留引用成员,更简单的做法是改成成员函数:

template<typename T>
class Vector<T, 3> : public Details::VectorBase<T, 3, Vector<T, 3>> {
public:
    T& x() { return this->values[0]; }
    const T& x() const { return this->values[0]; }
};

成员函数不是数据成员,不会触发“引用成员导致隐式赋值运算符 deleted”的规则;派生类可以继续使用隐式 copy/move assignment(前提是基类可赋值)。这也解释了你看到的差异:T& x() 没有问题,T& x 有问题。注意使用时要写 foo.x(),不能再用 foo.x。

需要注意的点

  • static_cast<D&>(*this) 成立的前提是 D 确实继承自当前 CRTP 基类,这是 CRTP 的基本约束;不要把基类单独实例化后依赖这个转换。
  • 基类的 operator=(const D&) 只是基类提供的一个额外运算符,不等于 D 的 copy assignment。要让 D 可赋值,仍需 D 自己的赋值运算符可用,或者移除导致隐式删除的成员。
  • 复制数组不要用 delete[] 和 new;对于 T values[N],直接用循环或 std::copy。
  • 如果只定义 copy assignment,临时对象仍可通过绑定到 const& 走 copy assignment;如果需要真正的移动语义,再显式定义 move assignment。
  • 保留引用成员时,务必检查 copy/move constructor,避免引用绑定到源对象。

备注:内容仅供参考。