在一个普通的可复制结构中,移动语义应该被实现吗?

In a trivially copyable struct shall the move semantics be implemented?

我有这样一个结构:

template <class T> struct Dimensions
{
    T horizontal{}, vertical{};

    Dimensions() = default;
    Dimensions(const T& horizontal, const T& vertical)
        : horizontal(horizontal), vertical(vertical) {}
    Dimensions(const Dimensions& other) = default;
    Dimensions& operator=(const Dimensions& other) = default;
    Dimensions(Dimensions&& other) = default; // ?
    Dimensions& operator=(Dimensions&& other) = default; // ?
    ~Dimensions() = default;

    // ... + - * / += -= *= areNull() ...

}

我将其实例化为 Dimensions<int>Dimensions<double>。由于它是 trivially copyable,这里最好的策略是什么,将移动构造函数和移动赋值运算符生成为 = default 或通过 = delete 避免隐式运算符?

generate the move constructor and move assignment operators as = default or avoid the implicit ones by = delete?

前者,除非您希望任何试图 std::move 您的类型的代码编译失败。例如

template <typename T>
void foo()
{
    T a;
    T b = std::move(a);
}

struct X
{
    X() = default;
    X(X&&) = delete;
};

int main() { foo<X>(); }

live example on wandbox.org