[英]In a trivially copyable struct shall the move semantics be implemented?
I have such a struct: 我有这样一个结构:
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() ...
}
which I instantiate like Dimensions<int>
or Dimensions<double>
. 我实例化了Dimensions<int>
或Dimensions<double>
。 Since it is trivially copyable , what would be the best policy here, generate the move constructor and move assignment operators as = default
or avoid the implicit ones by = delete
? 由于它是可以轻易复制的 ,这里最好的策略是什么,生成移动构造函数并将赋值运算符移动为= default
或者通过= delete
避免隐式运算符?
generate the move constructor and move assignment operators as
= default
or avoid the implicit ones by= delete
? 生成移动构造函数并将赋值运算符移动为= default
或通过= delete
避免隐式运算符?
The former, unless you want any code that attempts to std::move
your type to fail compilation. 前者,除非你想要任何试图std::move
你的类型以使编译失败的代码。 Eg 例如
template <typename T>
void foo()
{
T a;
T b = std::move(a);
}
struct X
{
X() = default;
X(X&&) = delete;
};
int main() { foo<X>(); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.