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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:32:03+00:00 2026-06-19T02:32:03+00:00

class C { public: int True(int i) const { return i+2; } }; const

  • 0
class C
{
public:
    int True(int i) const
    {
        return i+2;
    }
};


const C& F(const C& c)
{
    return c;
}

int main()
{ 
    const C& c = F(C());                             // Line 1
    cout << boolalpha << c.True(1) << endl;          // Line 2
}

Question> why the above code can print the value correct?
I assume the variable c will refer to an invalid temporary C object when it hits line 2.

// Update

I would like to update this OP to illustrate the reason why I am concern on this question.

Here is code snippet from C++ Templates: The Complete Guide

// maximum of two values of any type 
template <typename T> 
inline T const& max (T const& a, T const& b) 
{ 
    return a < b ? b : a; 
} 

As you can see the function return reference to the pass-in parameter.
I just wonder why not the following version instead:

// maximum of two values of any type 
template <typename T> 
inline T max (T const& a, T const& b) 
{ 
    return a < b ? b : a; 
} 
  • 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-19T02:32:04+00:00Added an answer on June 19, 2026 at 2:32 am

    Normally, when a temporary is bound to a const reference, the lifetime of the temporary is extended to the lifetime of the reference. Thus if your code said const C& c = C() then the temporary would live as long as c does.

    However, you’re passing the temporary to another function F(). In this case, §12.2.5 of the C++11 spec dictates that the temporary will persist until the completion of the full-expression containing the call.

    Therefore, when you say const C& c = F(C()), the temporary C() is actually destructed at the end of this statement, and is no longer valid on the next line.

    That said, your code appears to function properly because the call to c.True() is known at compile-time, and the function definition doesn’t actually refer to any data in c, so the fact that the temporary is dead doesn’t really affect the observed behavior. However this is technically undefined behavior.

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

Sidebar

Related Questions

class A { public: A() { cout << A() << endl; } A(const A&)
I have the following code: class STFDataPoint { public: virtual ImagePoint get_patch_top_left() const =
My class: public class Foo { public int A { get; set; } public
Given the class: public class Item { [Key] public int ID { get; set;
I have a class: public class LevelInfo { public int Id { get; set;
Consider the following class public class Class1 { public int A { get; set;
Say I have this class: public class Account { public int AccountID { get;
I have the following class: public class Account { public int AccountID { get;
I have the following class: public class Day { public int Date { get;
Except from my Rect-class: public class Rect { public int x; public int y;

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.