AutosarC++18_03-A8.4.5

“consume” parameters declared as X && shall always be moved from

Required inputs: IR

Rationale

A ”consume” parameter is declared with a type of rvalue reference to non-const non- template type (X &&). This documents that the value will be consumed in the function (i.e. left in a moved-from state) and requires an explicit ’std::move’ at the call site if an lvalue is passed to the function (an rvalue reference can implicitly bind only to an rvalue).

Note: Other operations may be performed on the ”consume” parameter before being moved.

Example
// $Id: A8-4-5.cpp 305588 2018-01-29 11:07:35Z michal.szczepankiewicz $

#include <string>
#include <vector>

class A {
public:
  explicit A(std::vector<std::string> &&v)
      : m_v{std::move(v)} // Compliant, move from consume parameter
  {}

private:
  std::vector<std::string> m_v;
};

class B {
public:
  explicit B(std::vector<std::string> &&v)
      : m_v{v} // Non-Compliant, consume parameter not moved from
  {}

  std::vector<std::string> m_v;
};
See Also
  • C++ Core Guidelines [10]: F.18: For ”consume” parameters, pass by X&& and std::move the parameter

Possible Messages

Key

Text

Severity

Disabled

missing_move_consume

“consume” parameters shall always be moved from

None

False

Options

exclude_move_assignments_and_constructors

exclude_move_assignments_and_constructors : bool = True

If set to true, "consume" parameters in move-assignments and move-constructors are not reported, even if they are not moved from.
 

moving_routines

moving_routines : set[bauhaus.analysis.config.QualifiedName] = set()

Qualified names of routines considered as moving, i.e., valid replacements for std::move.