std::move and lifetime of temporary objects

std::move and lifetime of temporary objects


11

Can someone explain the execution order of this code?

struct Foo {
    ~Foo() {
        std::cout << "1";
    }
};
int main() {
    const Foo& bar = Foo();
    const Foo& baz = std::move(Foo());
    std::cout << "2";
}

The following code prints 121.

I understand why I get 1 after 2, it’s because the lifetime of the object is bound to the code block where it executes and I also know that rvalue can bind to an lvalue const reference, but why destructor of the moved object is called immediately? What’s the reason for that? Where exactly is this destructor called?

Share
Improve this question

2

  • 3

    std::move(Foo()) (a.k.a. static_cast<Foo&&>(Foo()) is not a temporary object; binding a const reference to it does not extend its lifetime. An rvalue reference is not the same thing as an rvalue.

    – molbdnilo

    yesterday


  • 1

    Disappointingly, neither GCC nor Clang warn (directly) about the issue. They do both warn about baz being unused, while not complaining about bar since destruction has side-effects, which is an indirect clue, but not a good explanation 🙁

    – Matthieu M.

    5 hours ago

2 Answers
2

Reset to default


10

std::move has a forwarding reference parameter t that binds to the prvalue Foo(). Then when that function returns, that temporary is destroyed giving us the mentioned output.

Essentially the temporary is bound to parameter t instead of baz

//-------------------------------------------------------------------v------------> Foo() is bound to this parameter 
template< class T > constexpr std::remove_reference_t<T>&& move( T&& t ) noexcept;

Share
Improve this answer

1

  • It's destroyed not when the function returns, but later, when the initializer expression finishes executing.

    – HolyBlackCat

    3 hours ago


8

In std::move(Foo()); the Foo object is bound to the parameter of the move-function, not to baz.

And when the function returns, the temporary is destroyed.

Share
Improve this answer



Your Answer


Post as a guest

Required, but never shown


By clicking тАЬPost Your AnswerтАЭ, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged

or ask your own question.

Leave a Reply

Your email address will not be published. Required fields are marked *