AutosarC++19_03-A8.4.6¶
“forward” parameters declared as T && shall always be forwarded
Required inputs: IR
| Level | required |
|---|---|
| Target | design |
| Enforcement | automated |
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
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
incorrect_forward_used |
Wrong template type argument used in instantiation of std::forward |
None |
False |
only_forward |
Forwarding reference parameters shall always be forwarded. |
None |
False |
Options¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
check_template_type_argument_to_std_forward¶
check_template_type_argument_to_std_forward : bool = False
std::forward is appropriate for the parameter, i.e., the rule would
flag using forward as in the following example:
void foo( T && t1)
{
bar( std::forward< Other_Type_Than_T >( t1 ) );
}