【发布时间】:2020-12-16 06:38:14
【问题描述】:
考虑一个结构
template<typename T, size_t N>
struct Something {
std::array<T,N> args;
// Some constructors
};
现在让我们为Something<T> 重载= 运算符。事实上,我可以通过两种方式实现它。
第一道
Something& operator=(const Something& rhs){
// an implementation with copy semantics
}
Something& operator=(Something&& rhs) {
// an implementation with move semantics
}
第二种方式
Something& operator=(Something rhs){
// implement with move semantics
}
所以,我的问题是最标准的方式和最优化的方式来重载First Way或Second方式?
【问题讨论】:
-
您是在问一般情况,还是针对您的特定情况?一般来说,第二种方式可能不是最优的。
-
这取决于你的目的,指针引用和内存复制的选择
标签: c++ copy operator-overloading move-semantics rvalue