I have the following factory function:
auto factory() -> std::tuple<bool, std::vector<int>>
{
std::vector<int> vec;
vec.push_back(1);
vec.push_back(2);
return { true, vec };
}
auto [b, vec] = factory();
In the return statement is vec
considered an xvalue
or prvalue
and therefore moved or copy elided?
My guess is no, because the compiler, when list-initializing the std::tuple
in the return statement, still doesn't know that vec is going to be destroyed. So maybe an explicit std::move is required:
auto factory() -> std::tuple<bool, std::vector<int>>
{
...
return { true, std::move(vec) };
}
auto [b, vec] = factory();
Is it that really required?
return { true, std::move(vec) };
. – Aciculaallocator::construct/destruct
and the copy constructor of the elements ofvector
are all observable side effects. – Disable