次のように移動割り当てを使用して Matrix クラスを実装しました
template <typename OutType>
class Matrix
{
public:
int Rows_; // number of Rows
int Columns_; // number of Columns
OutType *data_; // row Major order allocation
// STUFF
Matrix<OutType> & operator=(Matrix<float>&& other) {
swap(other);
return *this;
}
void swap(Matrix<float>& other) {
int t_Rows_ = Rows_; Rows_ = other.Rows_; other.Rows_ = t_Rows_;
int t_Columns_ = Columns_; Columns_ = other.Columns_; other.Columns_ = t_Columns_;
float* t_ptr = data_;
data_ = other.data_;
other.data_ = t_ptr; }
}
B=f(A);
で提案されているように、構文を実装するために
C++: B および A 配列と B が既に定義されている B=f(A) の実装
可能な機能として、次のように実装されたFFTを検討しています
Matrix<float> FFT(const Matrix<float> &in)
{
Matrix<float> out(in.GetRows(),in.GetColumns());
// STUFF
return out;
}
さらなる効率改善の余地はありますか? swap
技の割り当てや機能など、さらに改善するコツはありますか?
編集: KONRAD RUDOLPH のコメントに続く新しいソリューション
Matrix & operator=(Matrix&& other) {
std::swap(Rows_, other.Rows_);
std::swap(Columns_, other.Columns_);
std::swap(data_, other.data_);
std::cout << "move assigned \n";
return *this;
}