例如,我一直密切关注永远不要写std::move
在退货声明中的建议。除了有一些边缘情况,例如.
我相信以下是另一个std::move
可能值得的简单示例-我错过了什么吗?但我不确定为什么,这在未来的 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;
}