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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:45:29+00:00 2026-06-09T17:45:29+00:00

I understand that c++ only allows rvalues or temp objects to bind to const-references.

  • 0

I understand that c++ only allows rvalues or temp objects to bind to const-references. (Or something close to that…)

For example, assuming I have the functions doStuff(SomeValue & input)
and SomeValue getNiceValue() defined:

/* These do not work */
app->doStuff(SomeValue("value1"));
app->doStuff(getNiceValue());

/* These all work, but seem awkward enough that they must be wrong. :) */

app->doStuff(*(new SomeValue("value2")));

SomeValue tmp = SomeValue("value3");
app->doStuff(tmp);

SomeValue tmp2 = getNiceValue();
app->doStuff(tmp2);

So, three questions:

  1. Since I am not free to change the signatures of doStuff() or getNiceValue(), does this mean I must always use some sort of “name” (even if superfluous) for anything I want to pass to doStuff?

  2. Hypothetically, if I could change the function signatures, is there a common pattern for this sort of thing?

  3. Does the new C++11 standard change the things at all? Is there a better way with C++11?

Thank you

  • 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-09T17:45:31+00:00Added an answer on June 9, 2026 at 5:45 pm

    An obvious question in this case is why your doStuff declares its parameter as a non-const reference. If it really attempts to modify the referred object, then changing function signature to a const reference is not an option (at least not by itself).

    Anyway, “rvalue-ness” is a property of an expression that generated the temporary, not a property of temporary object itself. The temporary object itself can easily be an lvalue, yet you see it as an rvalue, since the expression that produced it was an rvalue expression.

    You can work around it by introducing a “rvalue-to-lvalue converter” method into your class. Like, for example

    class SomeValue {
    public:
      SomeValue &get_lvalue() { return *this; }
      ...
    };
    

    and now you can bind non-const references to temporaries as

    app->doStuff(SomeValue("value1").get_lvalue());
    app->doStuff(getNiceValue().get_lvalue());
    

    Admittedly, it doesn’t look very elegant, but it might be seen as a good thing, since it prevents you from doing something like that inadvertently. Of course, it is your responsibility to remember that the lifetime of the temporary extends to the end of the full expression and no further.

    Alternatively, a class can overload the unary & operator (with natural semantics)

    class SomeValue {
    public:
      SomeValue *operator &() { return this; }
      ...
    };
    

    which then can be used for the same purpose

    app->doStuff(*&SomeValue("value1"));
    app->doStuff(*&getNiceValue());
    

    although overriding the & operator for the sole purpose of this workaround is not a good idea. It will also allow one to create pointers to temporaries.

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

Sidebar

Related Questions

I understand that a static function in C allows that particular function to only
Do I understand correctly that CCLabelBMFont only loads the font texture once, no matter
For reasons that only the developers can understand, Firefox will create and open .url
I understand that TempData is designed to work only between a single page request.
As I understand it, Domain Models are classes that only describe the data (aggregate
Why only one overload throws this exception? Little update: I understand that there was
The question says it all. I understand that VC11 is currently only in beta,
I have to connect to a poorly implemented server that only understands Content-Type (capital-T)
I have installed a script on my website that allows for a low contrast
I have an applet that allows users to sign documents using their personal certificates.

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.