Rule A8-4-5 (required, design, automated)

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

Rationale

A ”consume” parameter is declared with a type of rvalue reference to non-const nontemplate 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 [11]: F.18: For ”consume” parameters, pass by X&& and std::move the parameter