警告 C26479
请勿使用 std::move 返回局部变量。 (f.48)
注解
return
语句是局部变量的最后一个用法,因此编译器会尽可能使用移动语义来返回它。
在此方案中,添加 std::move
是冗余的。 此外,冗余 std::move
可以阻止复制传送。
代码分析名称:NO_MOVE_RET_ON_LOCALS
示例 1
S foo()
{
S local1{};
return std::move(local1); // Warning: C26479
}
若要解决此问题,请移除冗余 std::move
:
S foo()
{
S local1{};
return local1; // No warning
}
另请参阅
F.48 - 请勿返回 std::move(local)
ES.56 - 仅当需要将对象显式移动到另一个范围时写入 std::move()