18

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?

3
  • 5
    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
    Feb 7 at 11:35
  • 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 :( Feb 8 at 7:50
  • @molbdnilo: It is a temporary object. The reason there is no lifetime extension is that the reference is not "directly" bound to the temporary object.
    – Ben Voigt
    Feb 8 at 15:50

2 Answers 2

12

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.

11

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;
1
  • 1
    It's destroyed not when the function returns, but later, when the initializer expression finishes executing. Feb 8 at 9:24

Your Answer

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.