データのサイズとクラスの使用状況に応じて、データのコピーよりも移動セマンティクスの使用を検討し始める時期を知りたいと思っています。たとえば、Matrix4 クラスの場合、次の 2 つのオプションがあります。
struct Matrix4{
float* data;
Matrix4(){ data = new float[16]; }
Matrix4(Matrix4&& other){
*this = std::move(other);
}
Matrix4& operator=(Matrix4&& other)
{
... removed for brevity ...
}
~Matrix4(){ delete [] data; }
... other operators and class methods ...
};
struct Matrix4{
float data[16]; // let the compiler do the magic
Matrix4(){}
Matrix4(const Matrix4& other){
std::copy(other.data, other.data+16, data);
}
Matrix4& operator=(const Matrix4& other)
{
std::copy(other.data, other.data+16, data);
}
... other operators and class methods ...
};
「手動で」メモリの割り当てと割り当て解除を行う必要があるオーバーヘッドがあると思います。このクラスを使用するときに移動構造に実際にヒットする可能性があるとすれば、メモリサイズが非常に小さいクラスの推奨される実装は何ですか? 本当に常にコピーよりも優先される移動ですか?