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

”forward” parameters declared as T && shall always be forwarded.

Rationale

A ”forward” parameter is declared with a type of forwarding reference (i.e. an rvalue reference to non-const template type (T &&)). As a forwarding reference can bind to both lvalues and rvalues, preserving lvalue-ness and cv qualifications, it is useful when forwarding a value to another function using ”std::forward”. However, as the parameter can bind to anything, it should only be used for forwarding without performing any other operations on the parameter. Note: A forwarding parameter can also be declared via ”auto &&” in a generic lambda

Example

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

#include <string>
#include <vector>

class A
{
public:
explicit A(std::vector<std::string> &&v);
};

class B
{
public:
explicit B(const std::vector<std::string> &v);
};

template<typename T, typename ... Args>
T make(Args && ... args)
{
return T{std::forward<Args>(args) ...}; // Compliant, forwarding args
}

int main()
{
make<A>(std::vector<std::string>{ });

std::vector<std::string> v;
make<B>(v);

}

See also

C++ Core Guidelines [11]: F.19: For ”forward” parameters, pass by TP&& and only std::forward the parameter A18-9-2 in section 6.18.9