What exactly happens when returning const reference to a local object?

Viewed 4649
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)

2 Answers
Related