Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6379493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:08:44+00:00 2026-05-25T02:08:44+00:00

Update: clarification, more clear focus and shortened example: Can I circumvent the M op+(M&&,M&&)

  • 0

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+(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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:08:44+00:00Added an answer on May 25, 2026 at 2:08 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: giving a much more thorough example. The first two solutions offered were right
UPDATE: Focus your answers on hardware solutions please. What hardware/tools/add-in are you using to
UPDATE - A comprehensive comparison, updated as of February 2015, can be found here:
I want to let my database article reclassified. I have no good idea to
I can't seem to get this to work, Basically, what I want to do
Update: Solved, with code I got it working, see my answer below for the
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
Update : Looks like the query does not throw any timeout. The connection is
Update: Now that it's 2016 I'd use PowerShell for this unless there's a really
UPDATE: Thanks to everyone for the responses. I didn't realize document.write() was deprecated. Add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.