cppcoreguidelines-rvalue-reference-param-not-moved

当右值引用函数参数在函数体内从未移动时发出警告。

右值引用参数表示应该在函数体内使用 std::move 移动的参数。任何未被移动的此类参数都容易引起混淆,并且可能表明程序存在错误。

示例

void logic(std::string&& Input) {
  std::string Copy(Input); // Oops - forgot to std::move
}

请注意,标记为未使用的参数将不会被诊断。

示例

void conditional_use([[maybe_unused]] std::string&& Input) {
  // No diagnostic here since Input is unused and marked as such
}

选项

AllowPartialMove

如果设置为 true,则检查接受包含任何包含该参数的子表达式的 std::move 调用。CppCoreGuideline F.18 正式规定必须移动参数本身。默认值为 false

// 'p' is flagged by this check if and only if AllowPartialMove is false
void move_members_of(pair<Obj, Obj>&& p) {
  pair<Obj, Obj> other;
  other.first = std::move(p.first);
  other.second = std::move(p.second);
}

// 'p' is never flagged by this check
void move_whole_pair(pair<Obj, Obj>&& p) {
  pair<Obj, Obj> other = std::move(p);
}
IgnoreUnnamedParams

如果设置为 true,则检查将忽略未命名的右值引用参数。默认值为 false

IgnoreNonDeducedTemplateTypes

如果设置为 true,则检查将忽略非推导模板类型右值引用参数。默认值为 false

template <class T>
struct SomeClass {
  // Below, 'T' is not deduced and 'T&&' is an rvalue reference type.
  // This will be flagged if and only if IgnoreNonDeducedTemplateTypes is
  // false. One suggested fix would be to specialize the class for 'T' and
  // 'T&' separately (e.g., see std::future), or allow only one of 'T' or
  // 'T&' instantiations of SomeClass (e.g., see std::optional).
  SomeClass(T&& t) { }
};

// Never flagged, since 'T' is a forwarding reference in a deduced context
template <class T>
void forwarding_ref(T&& t) {
  T other = std::forward<T>(t);
}

此检查实现了 F.18,来自 C++ 核心准则。