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?
2
2 Answers
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;
1
-
It's destroyed not when the function returns, but later, when the initializer expression finishes executing.
– HolyBlackCat3 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.
Not the answer you're looking for? Browse other questions tagged
or ask your own question.
or ask your own question.
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.yesterday
Disappointingly, neither GCC nor Clang warn (directly) about the issue. They do both warn about
baz
being unused, while not complaining aboutbar
since destruction has side-effects, which is an indirect clue, but not a good explanation 🙁5 hours ago
|