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 1091739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:34:39+00:00 2026-05-16T23:34:39+00:00

Note: as noted by sellibitze I am not up-to-date on rvalues references, therefore the

  • 0

Note: as noted by sellibitze I am not up-to-date on rvalues references, therefore the methods I propose contain mistakes, read his anwser to understand which.

I was reading one of Linus’ rant yesterday and there is (somewhere) a rant against operator overloading.

The complaint it seems is that if you have an object of type S then:

S a = b + c + d + e;

may involve a lot of temporaries.

In C++03, we have copy elision to prevent this:

S a = ((b + c) + d) + e;

I would hope that the last ... + e is optimized, but I wonder how many temporaries are created with user defined operator+.

Someone in the thread suggested the use of Expression Templates to deal with the issue.

Now, this thread dates back to 2007, but nowadays when we think elimination of temporaries, we think Move.

So I was thinking about the set of overload operators we should write not to eliminate temporaries, but to limit the cost of their construction (stealing resources).

S&& operator+(S&& lhs, S const& rhs) { return lhs += rhs; }
S&& operator+(S const& lhs, S&& rhs) { return rhs += lhs; } // *
S&& operator+(S&& lhs, S&& rhs) { return lhs += rhs; }

Does this set of operator seems sufficient ? Is this generalizable (in your opinion) ?

*: this implementation supposes commutativity, it doesn’t work for the infamous string.

  • 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-16T23:34:39+00:00Added an answer on May 16, 2026 at 11:34 pm

    If you’re thinking about a custom, move-enabled string class, the proper way to exploit every combination of argument value categories is:

    S operator+(S const& lhs, S const& rhs);
    S operator+(S     && lhs, S const& rhs);
    S operator+(S const& lhs, S     && rhs);
    S operator+(S     && lhs, S     && rhs);
    

    The functions return a prvalue instead of an xvalue. Returning xvalues is usually a very dangerous thing – std::move and std::forward are the obvious exceptions. If you were to return an rvalue reference you’d break code like:

    for (char c : my_string + other_string) {
       //...
    }
    

    This loop behaves (according to 6.5.4/1 in N3092) as if the code is:

    auto&& range = my_string + other_string;
    

    This in turn results in a dangling reference. The temporary object’s life-time is not extended because your operator+ doesn’t return a prvalue. Returning the objects by value is perfectly fine. It’ll create temporary objects but these objects are rvalues, so we can steal their resources to make it very effective.

    Secondly, your code should also not compile for the same reason this won’t compile:

    int&& foo(int&& x) { return x; }
    

    Inside the function’s body x is an lvalue and you can’t initialize the “return value” (in this case the rvalue reference) with an lvalue expression. So, you’d need an explicit cast.

    Thirdly, you’re missing an const&+const& overload. In case both of your arguments are lvalues, the compiler won’t find a usable operator+ in your case.

    If you don’t want so many overloads, you could also write:

    S operator+(S value, S const& x)
    {
       value += x;
       return value;
    }
    

    I intentionally didn’t write return value+=x; because this operator probably returns an lvalue reference which would have led to copy construction of the return value. With the two lines I wrote the return value will be move constructed from value.

    S x = a + b + c + d;
    

    At least this case is very efficient because there is no unnecessary copying involved even if the compiler isn’t able to elide the copies – thanks to a move-enabled string class. Actually, with a class like std::string you can exploit its fast swap member function and make it effective in C++03 as well provided you have a reasonably smart compiler (like GCC):

    S operator+(S value, S const& x) // pass-by-value to exploit copy elisions
    {
       S result;
       result.swap(value);
       result += x;
       return result; // NRVO applicable
    }
    

    See David Abraham’s article Want Speed? Pass by Value. But these simple operators won’t be as effective given:

    S x = a + (b + (c + d));
    

    Here the left hand side of the operator is always an lvalue. Since operator+ takes its left hand side by value this leads to many copies. The four overloads from above deal perfectly with this example, too.

    It’s been a while since I read Linus’ old rant. If he was complaining about unnecessary copies with respect to std::string, this complaint is no longer valid in C++0x, but it was hardly valid before. You can efficiently concatenate many strings in C++03:

    S result = a;
    result += b;
    result += c;
    result += d;
    

    But in C++0x you can also use operator+ and std::move. This will be very efficient, too.

    I actually looked at the Git source code and its string management (strbuf.h). It looks well thought through. Except for the detach/attach feature you get the same thing with a move-enabled std::string with the obvious advantage that the resource it automatically managed by the class itself as opposed to the user who needs to remember to call the right functions at the right times (strbuf_init, strbuf_release).

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

Sidebar

Related Questions

Edit : Note that, as Daniel and latkin noted in an answer and a
Note, this is not a duplicate of .prop() vs .attr() ; that question refers
(Note: This is not a question about what is the best way with code
Note: I am not asking how to use Google Code's SVN repo as a
Note: Not sure if this is the right stack, please tell if I should
Note the tag: VBA , not VB6, not VB.NET. This is specific to VBA
I would like to set values on an url like this: <a href='http://$data_url/viewyournote.php?chapter='$name_of_chapter'&note='$id_note'&user='$username'> Then
note: i only want to move 1 shape at a time Circle.prototype.create = function(){
NOTE: This is a followup to my question here. I have a program that
NOTE: This is an old question and the answers here no longer works (since

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.