【发布时间】:2016-07-27 15:28:13
【问题描述】:
从 C++11 开始,我们有了移动语义。在下面的示例中,将使用移动构造函数(或复制省略),而不是 C++98 中的复制构造函数,无需任何额外的努力。
std::string f()
{
std::string res;
...
return res; // <- move is used here instead of copy
}
但是这个案子呢?
std::string f()
{
std::optional<std::string> res;
...
return *res; // <-- will the std::string value be moved??
}
还是必须写这样的东西?
std::string f()
{
std::optional<std::string> res;
...
return *std::move(res);
}
【问题讨论】:
-
你的意思是后面两个例子返回
string吗? -
是的。已编辑。谢谢!
标签: c++ move-semantics c++17