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

  • Home
  • SEARCH
  • 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: I've been playing around with this more, and it seems like tmux's clear-history
UPDATE: I'm getting this error: (No route matches /docs/index.html... ) when accessing admin.example.com/docs/index.html The
I can't seem to get this to work, Basically, what I want to do
Update: Is there a way to achieve what I'm trying to do in an
update: I mistyped 2 variables...so embarrassing. thanks everyone for the effort! sorry i find
Update: I reported this as a bug to Apple and they fixed it! All
Update : This is no longer an issue from C# 6, which has introduced
UPDATE UPI_ATTRIBUTE SET SITE_INC ='0' WHERE USER_PROFILING_NAME IN ('CAR_IMPLICIT','CAR_EXPLICIT') Above is my query that
Update: Switching to IIS Express instead of the Visual Studio Development Server fixed the
UPDATE 6/21/12 I have a form in rails that is working similar to an

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.