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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:48:17+00:00 2026-05-30T01:48:17+00:00

In order to stem the argument going on in the comments of an answer

  • 0

In order to stem the argument going on in the comments of an answer I gave recently, I’d like some constructive answers to the following questions:

  1. Is a reference’s lifetime distinct from the object it refers to? Is a reference simply an alias for its target?
  2. Can a reference outlive its target in a well-formed program without resulting in undefined behaviour?
  3. Can a reference be made to refer to a new object if the storage allocated for the original object is reused?
  4. Does the following code demonstrate the above points without invoking undefined behaviour?

Example code by Ben Voigt and simplified (run it on ideone.com):

#include <iostream>
#include <new>

struct something
{
    int i;
};

int main(void)
{
    char buffer[sizeof (something) + 40];
    something* p = new (buffer) something;
    p->i = 11;
    int& outlives = p->i;
    std::cout << outlives << "\n";
    p->~something(); // p->i dies with its parent object
    new (p) char[40]; // memory is reused, lifetime of *p (and p->i) is so done
    new (&outlives) int(13);
    std::cout << outlives << "\n"; // but reference is still alive and well
                                   // and useful, because strict aliasing was respected
}
  • 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-30T01:48:18+00:00Added an answer on May 30, 2026 at 1:48 am

    Is a reference’s lifetime distinct from the object it refers to? Is a reference simply an alias for its target?

    A reference has its own lifetime:

    int x = 0;
    {
       int& r = x;
    }      // r dies now
    x = 5; // x is still alive
    

    A ref-to-const additionally may extend the lifetime of its referee:

    int foo() { return 0; }
    const int& r = foo();   // note, this is *not* a reference to a local variable
    cout << r;              // valid; the lifetime of the result of foo() is extended
    

    though this is not without caveats:

    A reference to const only extends the lifetime of a temporary object if the reference is a) local and b) bound to a prvalue whose evaluation creates said temporary object. (So it doesn’t work for members, or local references which are bound to xvalues.) Also, non-const rvalue references extend the lifetime in the exact same fashion. [@FredOverflow]


    Can a reference outlive its target in a well-formed program without resulting in undefined behaviour?

    Sure, as long as you don’t use it.


    Can a reference be made to refer to a new object if the storage allocated for the original object is reused?

    Yes, under some conditions:

    [C++11: 3.8/7]: If, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied, a pointer that pointed to the original object, a reference that referred to the original object, or the name of the original
    object will automatically refer to the new object and, once the lifetime of the new object has started, can be used to manipulate the new object, if:

    • the storage for the new object exactly overlays the storage location which the original object occupied, and
    • the new object is of the same type as the original object (ignoring the top-level cv-qualifiers), and
    • the type of the original object is not const-qualified, and, if a class type, does not contain any non-static data member whose type is const-qualified or a reference type, and
    • the original object was a most derived object (1.8) of type T and the new object is a most derived object of type T (that is, they are not base class subobjects).

    Does the following code demonstrate the above points without invoking undefined behaviour?

    Tl;dr.

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

Sidebar

Related Questions

Order by descending is not working on LINQ to Entity In the following Query
With the following simple relational database structure: An Order has one or more OrderItems,
I have the following layout: Entities: Order OrderItem DAO classes: OrderDAO OrderItemDAO So I
Using the following database table structure: Order Table: OrderId OrderName OrderItem Table: OrderId ItemId
I need some help with sending email when an order is placed. To illustrate
If i have the following classes public class Order { public virtual ISet<OrderItem> OrderItems
Trying to do something like: public interface Order { public List<? extends OrderItem> getItems();
Maybe I'm going about this the wrong way... I have an Order table and
I have the following tables: Order ---- ID (pk) OrderItem ---- OrderID (fk ->
What is the best practice to implement the following: we have a classes Order

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.