I've been paying close attention to the advice never to write std::move
in a return statement, for example. Except there are some edge cases, for example.
I believe the following is another simple example of where std::move
may be worthwhile - did I miss something? But I'm not sure why, and will that change in a future C++?
#include <iostream>
struct A
{
};
struct C
{
};
struct B
{
B(const A&, const C&) { std::cout << "B was copied\n"; }
B(A&&, C&&) { std::cout << "B was moved\n"; }
};
B f()
{
A a;
C c;
//return {a, c}; // Gives "B was copied"
return {std::move(a), std::move(c)}; // Gives "B was moved"
}
int main() {
f();
return 0;
}