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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:09:45+00:00 2026-06-08T18:09:45+00:00

struct A { A(int) : i(new int(783)) { std::cout << a ctor << std::endl;

  • 0
struct A {
    A(int) : i(new int(783)) {
        std::cout << "a ctor" << std::endl;
    }

    A(const A& other) : i(new int(*(other.i))) {
        std::cout << "a copy ctor" << std::endl;
    }

    ~A() {
        std::cout << "a dtor" << std::endl;
        delete i;
    }

    void get() {
        std::cout << *i << std::endl;
    }

private:
    int* i;
};

const A& foo() {
    return A(32);
}

const A& foo_2() {
    return 6;
}

int main()
{
    A a = foo();
    a.get();
}

I know, returning references to local values is bad. But, on the other hand, const reference should extend a temporary object lifetime.

This code produce an UB output. So no life extention.

Why? I mean can someone explain whats happening step by step?

Where is fault in my reasoning chain?

foo():

  1. A(32) – ctor

  2. return A(32) – a const reference to local object is created and is returned

  3. A a = foo(); – a is initialized by foo() returned value, returned value goes out of scope(out of expression) and is destroyed, but a is already initialized;

(But actually destructor is called before copy constructor)

foo_2():

  1. return 6 – temp object of type A is created implicitly,a const reference to this object is created(extending its life) and is returned

  2. A a = foo(); – a is initialized by foo() returned value, returned value goes out of scope(out of expression) and is destroyed, but a is already initialized;

(But actually destructor is called before copy constructor)

  • 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-08T18:09:46+00:00Added an answer on June 8, 2026 at 6:09 pm

    Rules of temporary lifetime extension for each specific context are explicitly spelled out in the language specification. And it says that

    12.2 Temporary objects

    5 The second context is when a reference is bound to a temporary. […] A temporary bound to the returned value in a function return statement
    (6.6.3) persists until the function exits. […]

    Your temporary object is destroyed at the moment of function exit. That happens before the initialization of the recipient object begins.

    You seem to assume that your temporary should somehow live longer than that. Apparently you are trying to apply the rule that says that the temporary should survive until the end of the full expression. But that rule does not apply to temporaries created inside functions. Such temporaries’ lifetimes are governed by their own, dedicated rules.

    Both your foo and your foo_2 produce undefined behavior, if someone attempts to use the returned reference.

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

Sidebar

Related Questions

Having code: struct B { int* a; B(int value):a(new int(value)) { } B():a(nullptr){} B(const
struct Foo { int data; Foo() = default; Foo(const Foo& arg) = default; };
struct Foo { Foo(int i) { ptr = new int(i); } ~Foo() { delete
In the following: struct adt { void * A; }; int new_adt(const void *
I have structs like this: struct A { int a; virtual void do_stuff(A*a) {
#include <iostream> using namespace std; struct node { int v; node* next; node (int
I have a Deque that contains this kind of stucts. struct New_Array { array<array<int,4>,4>
Consider the following: typedef struct { int a; int b; int c; int d;
I have a struct typedef struct { int id; string name; string address; string
Java noob question: Consider the following C array and initializer code: struct { int

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.