operator-overloading - C++11 重载 `M operator+(M&&,M&&)`

标签 operator-overloading move-semantics c++11

更新:澄清、更明确的重点和缩短的示例:

  • 我可以避免 M op+(M&&,M&&) 过载吗?假设,我想很好地处理 RValues?我想其他三个重载必需的。

我首先使用 (&&,&&) 重载的原因:

  • 通常我不会提供M op+(&&,&&),但我似乎需要它:当为(&&,&)提供重载时(&,&&) 编译器陷入歧义。有没有比添加另一个实现变体更好的解决方法?

您还可以查看 complete代码。

struct Matrix {
...
  // 2ary ops
  friend Matrix operator+(const Matrix &a, Matrix &&b     ) { b+=a; return move(b); }
  friend Matrix operator+(Matrix &&a,      const Matrix &b) { a+=b; return move(a); }
  friend Matrix operator+(const Matrix &a, Matrix v)        { v+=a; return v; }
  friend Matrix operator+(Matrix &&a,      Matrix &&b)      { a+=b; return move(a); }
  // ... same for operator*
  // ... assume impl of operator+=,*= and move semantics
};

int main() {
  Matrix a{2},b{3},c{4},d{5};
  Matrix x = a*b + c*d;  // reuires &&,&& overload
  std::cout << x << std::endl;
}

最佳答案

如果第一个值是右值,则以下辅助函数返回第一个值,否则返回第二个值(可能是右值,但也可能不是)。

template <class T1, class T2>
typename std::enable_if<! std::is_reference<T1>::value, T1&&>::type 
  get_rvalue(T1&& t1, T2&& t2) { return std::forward<T1>(t1); }

template <class T1, class T2>
typename std::enable_if<std::is_reference<T1>::value, T2&&>::type 
  get_rvalue(T1&& t1, T2&& t2) { return std::forward<T2>(t2); }     

下面的辅助函数返回上面没有返回的其他值。

template <class T1, class T2>
typename std::enable_if<! std::is_reference<T1>::value, T1&&>::type 
  get_non_rvalue(T1&& t1, T2&& t2) { return std::forward<T2>(t2); }

template <class T1, class T2>
typename std::enable_if<std::is_reference<T1>::value, T2&&>::type 
  get_non_rvalue(T1&& t1, T2&& t2) { return std::forward<T1>(t1); }

这只是比较两种类型是否相同,忽略引用和常量。

template <class T1, class T2>
struct is_same_decay : public std::is_same<
  typename std::decay<T1>::type, 
  typename std::decay<T2>::type
> {};

然后我们可以像下面这样(使用模板)为每个函数做一个重载:

// 2ary ops
template <class M1, class M2>
friend typename std::enable_if< 
  is_same_decay<M1, Matrix>::value &&
  is_same_decay<M2, Matrix>::value,
Matrix>::type
operator+(M1&& a, M2&& b) 
{ 
  Matrix x = get_rvalue(std::forward<M1>(a), std::forward<M2>(b)); 
  x += get_non_rvalue(std::forward<M1>(a), std::forward<M2>(b)); 
  return x; 
}

template <class M1, class M2>
friend typename std::enable_if< 
  is_same_decay<M1, Matrix>::value &&
  is_same_decay<M2, Matrix>::value,
Matrix>::type
operator*(M1&& a, M2&& b) 
{ 
  Matrix x = get_rvalue(std::forward<M1>(a), std::forward<M1>(b)); 
  x *= get_non_rvalue(std::forward<M1>(a), std::forward<M1>(b)); 
  return x; 
}

注意上面,如果 M1M2 是一个右值,get_rvalue(a, b) 将返回一个右值,因此在这个case Matrix x 将由 move 而不是副本填充。命名返回值优化可能会确保不需要复制(甚至 move )到返回值中,因为 x 将在返回值的位置构造。

完整代码是 here .

关于operator-overloading - C++11 重载 `M operator+(M&&,M&&)`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7173498/

相关文章:

C++ 使用 *this 属性调用单个辅助函数

c++ - 为什么 std::vector 使用 move 构造函数,尽管声明为 noexcept(false)

c++ - 自动生成的 move 构造函数,成员不可 move

c++ - 如何创建具有任意数量(编译时确定)容器的内存池?

c++ - 简单的 C++ 运算符重载帮助

c# - true 和 false 的运算符重载如何工作?

c++11 - C++11 中返回本地值的最佳方式

c++ - 可以替换 std::allocator 允许 std::set 使用 void* 和单独的复制/释放函数吗?

c++ - 在 C++11 中使用用户定义的转换重载运算符推导

C++使用模板函数,在主函数中定义一个具有ostream类变量的构造函数