实施B = F(A)语法由移动分配 [英] Implementing the B=f(A) syntax by move assignment
问题描述
我已经实现了移动分配矩阵类
模板< typename的OutType>
类矩阵
{
上市:
INT Rows_; //行数
INT列_; //列数
OutType * data_中的数据; //行优先顺序分配 // 东东 矩阵LT; OutType> &安培;运算符=(矩阵<浮球GT;&放大器;&安培;其他){
掉期(其他);
返回*这一点;
} 无效掉期(矩阵<浮球GT;&安培;其他){
INT t_Rows_ = Rows_; Rows_ = other.Rows_; other.Rows_ = t_Rows_;
INT t_Columns_ =列_;列_ = other.Columns_; other.Columns_ = t_Columns_;
浮动* t_ptr = data_中的数据;
data_中的数据= other.data_;
other.data_ = t_ptr; }
}
为了贯彻落实 B = F(A);
语法,如
<一个href=\"http://stackoverflow.com/questions/16254797/c-implementing-b-fa-with-b-and-a-arrays-and-b-already-defined\">C++:实施B = F(A),与已
定义B和A阵列和B由于可能的功能,我考虑的FFT,因为
实施 矩阵和LT;浮动&GT; FFT(常量矩阵和LT;浮动&GT;&安培;中)
{
矩阵LT;浮动&GT;出(in.GetRows(),in.GetColumns()); // 东东 返回的;
}
有没有任何余地进一步提高效率?是否有任何进一步的伎俩来提高,例如,移动分配或交换
功能?
编辑:新的解决方案FOLLOWING KONRAD鲁道尔夫的评论
矩阵和放大器;运算符=(矩阵和放大器;&安培;其他){
的std ::互换(Rows_,other.Rows_);
的std ::互换(栏_,other.Columns_);
的std ::互换(data_中的数据,other.data_);
性病::法院LT&;&LT; 移动分配\\ N的;
返回*这一点;
}
我建议实施布展分配和布展施工类:
矩阵(矩阵&LT; OutType&GT;&放大器;&安培;那)noexcept
:Rows_(that.Rows_)
,Cols_(that.Cols_)
,data_中的数据(that.data_)
{
that.Rows_ = that.Cols_ = 0;
that.data_ = nullptr;
}
矩阵LT; OutType&GT; &放大器;运算符=(矩阵&LT; OutType&GT;&放大器;&安培;那)noexcept {
使用std ::交换;
掉期(Rows_,that.Rows_);
掉期(Cols_,that.Cols_);
掉期(data_中的数据,that.data_);
返回*这一点;
}
如果您实现移动业务(建设,转让),这样,的std ::互换
应该工作非常适合你的code,并且不需要提供你自己的。如果你想提供自己的交换
,我建议提供它作为两个参数朋友
函数,所以它可以通过参数相关的查找找到。我还建议呼吁交换
(和所有其他功能)没有命名空间的资格,如上图所示,使ADL不燮pressed(除非出于某种原因,你真的需要指定到底哪个函数被调用,和定制的特定类型的重载将是错误的)。 ADL用模板code打交道时是特别有价值。如果你调用的std ::互换
与的std ::
预选赛中,你显著减少用户定义类型的机会以提供更有效的交换实施
I have implemented a Matrix class with a move assignment as
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; }
}
in order to implement the B=f(A);
syntax, as suggested in
C++: Implementing B=f(A), with B and A arrays and B already defined
As possible function, I'm considering the FFT, implemented as
Matrix<float> FFT(const Matrix<float> &in)
{
Matrix<float> out(in.GetRows(),in.GetColumns());
// STUFF
return out;
}
Is there any room for further efficiency improvements? Is there any further trick to improve, for example, the move assignment or the swap
function?
EDIT: NEW SOLUTION FOLLOWING KONRAD RUDOLPH'S COMMENT
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;
}
I recommend implementing move-assignment and move-construction for your class:
Matrix( Matrix<OutType> &&that ) noexcept
: Rows_(that.Rows_)
, Cols_(that.Cols_)
, data_(that.data_)
{
that.Rows_ = that.Cols_ = 0;
that.data_ = nullptr;
}
Matrix<OutType> &operator=( Matrix<OutType> &&that ) noexcept {
using std::swap;
swap( Rows_, that.Rows_ );
swap( Cols_, that.Cols_ );
swap( data_, that.data_ );
return *this;
}
If you implement move operations (construction and assignment) like this, std::swap
should work great for your code, and you don't need to provide your own. If you do want to provide your own implementation of swap
, I recommend providing it as a two-argument friend
function so that it can be found through Argument Dependent Look-up. I also recommend calling swap
(and all other functions) without namespace qualifications, as shown above, so that ADL is not suppressed (unless, for some reason, you really need to specify exactly which function is called, and an overload customized for the specific type would be wrong). ADL is especially valuable when dealing with templated code. If you call std::swap
with the std::
qualifier, you significantly reduce the opportunity for user-defined types to provide a more efficient swap implementation.
这篇关于实施B = F(A)语法由移动分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!