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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:02:51+00:00 2026-06-15T15:02:51+00:00

I have a string class. Some operators return references, others return values. Only the

  • 0

I have a string class. Some operators return references, others return values. Only the ones that return values can take advantage of the rvalue copy constructor or rvalue assignment operator.

I would like the rvalue operator to be called on a reference to an rvalue.

Given these:

String(const TCHAR* sz);
String(const String& s);    
String& operator+=(const TCHAR* sz);    
String& operator=(String&& r);

And this code:

String x;
x = (String("fred") += "foo");

It calls the copy constructor, the += operator, but then the COPY CONSTRUCTER again. I want it to call the rvalue assignment operator!

I added this:

String(String&& r) 

And that makes no difference.

EDIT:
I confirmed that if I make my += operator return a value, the rvalue assignment operator will be called. I have done a lot of performance testing and doing this makes everything a lot slower.

  • 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-06-15T15:02:52+00:00Added an answer on June 15, 2026 at 3:02 pm

    You can’t easily differentiate between a “genuine” rvalue and an rvalue reference, but you don’t seem to have a motivation to do so. Your overload is fine.

    The problem is that rvalue-ness is lost by the += operator. There are three solutions:

    1. Manually make it an rvalue: x = std::move(String("fred") += "foo");
    2. Use non-member overloads. I generally prefer this because it resolves other issues such as applying conversion functions to the left-hand side of a += expression.

      String& operator+=(String &lhs, const TCHAR* sz);
      String&& operator+=(String &&lhs, const TCHAR* sz);
      
    3. Use reference qualifiers so that operator+= returns an rvalue when applied to an rvalue. Few compilers support this yet.

      String& operator+=(const TCHAR* sz) &;
      String&& operator+=(const TCHAR* sz) &&;
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a static util class that does some string manipulation on a bit
I have some java classes like, Class User { String email; String name; String
I have some custom type: [RdfSerializable] public class Item { [RdfProperty(true)] public string Name
I have class Vertex{ Graph _graph; float x; float y; string key; //and some
can you please help me with this issue the String class does not have
I have a class called: ComplexNumber and I have a string that I need
I have a static std::map<std::string, CreateGUIFunc> in a class that basically holds strings identifying
I have a URL class that overloads the ==, <, >, and != operators
I have some problems overloading operators with template members and using make_pair: class MyArchive
I have this code, CBString is just a string class I use for some

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.