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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:27:17+00:00 2026-06-11T21:27:17+00:00

Consider the following code dealing with const references: const int & func (const int

  • 0

Consider the following code dealing with const references:

const int & func (const int &x)
{
    return x;
}

struct Foo {
    Foo (const int &x)
    : m_x(x) {}

    const int & getX ()
    { return m_x; }

    const int &m_x;
};

I’d like to know which, if any, of the following is now allowed:

int x = func(int(7));
int y = Foo(int(7)).getX();

Is there any guarantee that the temporary int object still exists before it is used by the assignment or getX?.

UPDATE: So it appears this is safe – but why exactly?

  1. Is it because temporaries bind to const references recursively and are guaranteed to exist as long as of those bound references to them exists?
  2. Or is it because they are guaranteed to exist for the duration of the full expression?

Consider the edge case which stores a pointer instead of a reference:

struct Foo {
    Foo (const int &x)
    : m_x(&x) {}

    const int & getX ()
    { return *m_x; }

    const int *m_x;
};

int y = Foo(int(7)).getX();

It seems that if case 1) was correct, this would not work. But if case 2) was correct, it would.

  • 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-11T21:27:18+00:00Added an answer on June 11, 2026 at 9:27 pm

    Both are safe, because you copy the values into x and y. The temporaries are valid until the end of the full expression.

    12.2 Temporary objects

    4) There are two contexts in which temporaries are destroyed at a
    different point than the end of the full-expression. The first context
    is when a default constructor is called to initialize an element of an
    array. If the constructor has one or more default arguments, any
    temporaries created in the default argument expressions are destroyed
    immediately after return from the constructor.

    5) The second context is
    when a reference is bound to a temporary. The temporary to which the
    reference is bound or the temporary that is the complete object of a
    subobject to which the reference is bound persists for the lifetime of
    the reference except as specified below. A temporary bound to a
    reference member in a constructor’s ctor-initializer (12.6.2) persists
    until the constructor exits. A temporary bound to a reference
    parameter in a function call (5.2.2) persists until the completion of
    the full expression containing the call. A temporary bound to the
    returned value in a function return statement (6.6.3) persists until
    the function exits. In all these cases, the temporaries created during
    the evaluation of the expression initializing the reference, except
    the temporary to which the reference is bound, are destroyed at the
    end of the full-expression in which they are created and in the
    reverse order of the completion of their construction. If the lifetime
    of two or more temporaries to which references are bound ends at the
    same point, these temporaries are destroyed at that point in the
    reverse order of the completion of their construction. In addition,
    the destruction of temporaries bound to references shall take into
    account the ordering of destruction of objects with static or
    automatic storage duration (3.7.1,
    3.7.2); that is, if obj1 is an object with static or automatic storage duration created before the temporary is created, the temporary shall
    be destroyed before obj1 is destroyed; if obj2 is an object with
    static or automatic storage duration created after the temporary is
    created, the temporary shall be destroyed after obj2 is destroyed. [
    Example:

    class C 
    { 
    / / ... 
    public : 
        C(); 
        C(int ); 
        friend C operator +(const C&, const C&); 
        ~C(); 
    }; 
    C obj1 ; 
    const C& cr = C (16)+ C (23); 
    C obj2 ; 
    

    the
    expression C(16)+C(23) creates three temporaries. A first temporary T1
    to hold the result of the expression C(16), a second temporary T2 to
    hold the result of the expression C(23), and a third temporary T3 to
    hold the result of the addition of these two expressions. The
    temporary T3 is then bound to the reference cr. It is unspecified
    whether T1 or T2 is created first. On an implementation where T1 is
    created before T2, it is guaranteed that T2 is destroyed before T1.
    The temporaries T1 and T2 are bound to the reference parameters of
    operator+; these temporaries are destroyed at the end of the full
    expression containing the call to operator+. The temporary T3 bound to
    the reference cr is destroyed at the end of cr’s lifetime, that is, at
    the end of the program. In addition, the order in which T3 is
    destroyed takes into account the destruction order of other objects
    with static storage duration. That is, because obj1 is constructed
    before T3, and T3 is constructed before obj2, it is guaranteed that
    obj2 is destroyed before T3, and that T3 is destroyed before obj1.
    —end example ]

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

Sidebar

Related Questions

Consider the following code: struct Foo { Foo operator+(const Foo &rhs) const; // notice
Consider the following code: struct Foo { }; struct Bar { explicit Bar(const Foo&)
Consider the following code: template <class x1, class x2 = int*> struct CoreTemplate {
Consider the following code : template<typename T> class Base { Base(); Base(const Base<T>& rhs);
Consider the following code in C: void main() { int a=0; for(printf(\nA); a; printf(\nB));
Consider the following code: #include <iostream> using namespace std; class Test { static int
The following code is illegal: public struct MyStruct { public MyStruct(int a, int b)
Consider the following code: function authenticate(){ $this->session->set_userdata('logged_in', true); } How do I return this
Consider the following code: #include <iostream> struct test { void public_test() { [this]() {
Consider following code public class City { public string Name { get { return

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.