返回时隐式移动std::optional中包含的值
Implicit move the value contained in std::optional when return
自从C++11以来,我们就有了移动语义。在下面的例子中,将使用move构造函数(或复制省略),而不是像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);
}
否。隐式移动的标准在[class.copy]中:
当[…],或者当
return
语句中的表达式是(可能带括号)id表达式,该表达式命名具有在最内部封闭函数的正文或参数声明子句中声明的自动存储持续时间的对象,或lambda表达式
*res
不是id表达式,因此此规则不适用。如果你想移出底层字符串,你必须明确地这样做:
return std::move(*res);
return *std::move(res);
return std::move(res).value(); // equivalent and possibly more legible
这些规则旨在只有在绝对安全的情况下才尝试更有效的选项。如果你返回一个自动存储持续时间变量,那么移动它是完全安全的,因为没有其他变量会再次引用该变量。
但是,如果您要返回*res
,则不一定安全。如果这给了你一个对某个外部对象的引用,它将比这个函数更长寿呢?我们将默默地离开我们期望仍然有效的状态!在这种情况下,由您作为用户声明您希望它被移动。
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 编译包含字符串的代码时遇到问题
- c++库的公共头文件中应该包含什么
- 将包含C样式数组的对象初始化为成员变量(C++)
- 是否需要删除包含对象的"pair"?
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 使用mongocxx驱动程序时包含头文件问题
- 如何在h文件中包含.o对象文件
- 在混合代码库中将C转换为C++时出现许多包含错误
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- cmath抛出错误C2062、C2059、C2143和C2447.cmath包含在矢量文件中
- 为什么您需要C++头文件的包含保护
- 无法在UE4中包含BP类到CPP类
- g++ 说函数不存在,即使包含正确的标头
- 在C++代码中包含opencv时,使用ctypes创建.so文件
- C++17:使用 std::optional 来评估枚举是否包含值
- 如果包含的类型是可简单复制的类型,则 std::Optional 是否为可平凡复制的类型
- 返回时隐式移动std::optional中包含的值