开发者

C++11 overload of `M operator+(M&&,M&&)`

Update: clarification, more clear focus and shortened example:

  • Can I circumvent the M op+(M&&,M&&) overload? Assuming, I want good handling of RValues? I guess the other three overloads are required.

Reason why I have the (&&,&&) overload in the first place:

  • Normally I would not provide M op+(&&,&&), but I seem to need it: When providing overloads for (&&,&) and (&,&&) the compiler gets into an ambiguity. Is there a better way to resolve it then to add another implementation variant?

You can also look at the complete code.

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+(con开发者_Go百科st 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;
}


The following helper function returns the first value if it an rvalue, otherwise the second value (which may be an rvalue, but may not be).

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); }     

The following helper function returns the other value not returned above.

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); }

This just compares if two types are the same, ignoring references and const.

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

Then we can do just one overload for each function (using templates) like the following:

// 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; 
}

Note above, if either M1 or M2 is an rvalue, get_rvalue(a, b) will return an rvalue, hence in this case Matrix x will be populated by a move, not a copy. Named return value optimisation will probably ensure that there is no copy (or even move) required into the return value, as x will be constructed in the place of the return value.

Full code is here.


Matrix& operator=(Matrix&& o) { swap(*this,o); };

First, your Matrix class has nothing (as of yet) that needs moving, so you shouldn't bother writing one. Like a copy constructor, only define one if you need to. Let the compiler take care of it unless you have a legitimate need (like storing a naked pointer).

Second, that function of yours doesn't move; it swaps. An "idiomatic" swap-based move involves a temporary, like this:

Matrix temp;
swap(o, temp);
swap(temp, *this);

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); }

What are you trying to accomplish here? Again, your object doesn't have anything that gets moved; there's no point in doing this. Just because you could move something, doesn't mean you should. If you really wanted to cut down on code duplication, you would do things normally:

friend Matrix operator+(const Matrix &a, const Matrix &b) { Matrix temp = a + b; return temp; }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜