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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:59:17+00:00 2026-05-12T16:59:17+00:00

Is it possible to change the temporary object and to pass it as an

  • 0

Is it possible to change the temporary object and to pass it as an argument?

struct Foo {    
   Foo& ref() { return *this; }
   Foo& operator--() { /*do something*/; return *this; }
   // another members
};
Foo getfoo() { return Foo(); } // return Foo() for example or something else
void func_val(Foo x) {}
void func_ref(const Foo & x) {}

int main() {
   func_val(--getfoo());     // #1 OK?
   func_ref(getfoo());       // #2 OK?
   func_ref(getfoo().ref()); // #3 OK?
   // the following line is a real example 
   //    using --vector.end() instead of --getfoo()
   func_ref(--getfoo());     // #4 OK? 

   const Foo & xref = --getfoo(); //  Does const extend the lifetime ?
   func_ref(xref);     // #5 OK? 
   func_val(xref);     // #6 OK? 

}

It is known that assigning a temporary object to the const reference extends the lifetime of this temporary object. And what about #4 and #5 lines of my code?
Is it true that reference x is always valid in the function func_ref?
The thing is that operator– returns some reference and the compiler does not see any relation between this reference and the temporary we created.

  • 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-12T16:59:17+00:00Added an answer on May 12, 2026 at 4:59 pm
    func_val(--getfoo());     // #1 OK?
    

    Yes, OK. The operator-- is a member-function, which is called, and which returns itself (and lvalue referring to itself). The object is then copied into the parameter of func_val. Notice that return value optimization is not allowed to apply, since the temporary created by getfoo() was previously bound to a reference.

    func_ref(getfoo());       // #2 OK?
    

    Yes, OK. The call getfoo() returns a temporary which is bound to the const reference. A copy constructor is required, but the call it it may be optimized out by the implementation. The temporary persists until the end of the full-expression containing the call to func_ref (the whole expression statement here).

    func_ref(getfoo().ref());
    

    Yes, OK. No copy constructor required, as we bind the const reference not to a temporary but to the lvalue representing the object itself.

    // the following line is a real example 
    //    using --vector.end() instead of --getfoo()
    

    That is not required to work. Think of a situation where vector.end() returns a T* (allowed). You are not allowed to modify rvalues of non-class type, so in that case, this would be ill-formed.

    func_ref(--getfoo()); 
    

    Yes, OK. The argument is evaluated as in #1, but the resulting lvalue is directly passed and the const reference is bound to it. In this sense it’s equal to #3 (except for the decrement side effect).

    const Foo & xref = --getfoo();
    

    The Standard wording is not entirely clear. It surely intends to only extend lifetime of objects not yet bound to a reference. But in our case, --getfoo() yields an lvalue refering to a temporary object which was previously bound to a reference. It may be worth submitting a defect report to the committee (i may also have missed wording that requires the temporary object to not be bounded to a reference yet).

    In any case, the intended behavior is to destruct the temporary that results from getfoo() at the end of initializing xref, so xref will become a dangling reference.

    The thing is that operator– returns some reference and the compiler does not see any relation between this reference and the temporary we created.

    Exactly (but applies only to the initialization of xref which will go mad. In all other cases, the intended behavior you want (or what i believe you want) is achieved).

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

Sidebar

Related Questions

I want to change the attributes of an existing element, and not just the
I have this query SELECT l.licitatii_id, l.nume, l.data_publicarii, l.data_limita FROM licitatii_ue l INNER JOIN
From my understanding of how cloning works, when a change to a parent of
I'm looking for a piece of code which behaves a bit like a singleton
Summarization: For a Delphi function/procedure, if an instance of a class is passed through
I have a couple of hidden inputs on an asp.net mvc view. Their values
for my bachelor thesis I have written a program (in C) to sort large
Does a feature like TFS auto-checkout before checkin exist, so that I don't checkout
I have a couple of lines of code that work if use strict; is
I'm trying to configure Team Foundation Reporting but without any success. The App Tier

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.