AUTOSAR C++14 Rule A18-9-4
Description
Rule Definition
An argument to std::forward shall not be subsequently used.
Rationale
You typically use std::forward
in a function template to pass a
forwarding reference parameter to another function. The resources of the parameter might be
transferred to another object through a move operation, depending on the value category of
the parameter.
For an rvalue parameter, the parameter is in an indeterminate state if it is moved from
after the call to std::forward
and it should not be reused.
For an lvalue parameter, If you reuse the parameter after the call to
std::forward
, modifications to the parameter might affect the argument
of the caller function to which you pass the parameter.
Polyspace Implementation
Polyspace® flags the call to std::forward
if the forwarded object is
reused after the call. Polyspace also highlights the lines where the forwarded object is reused in your
code.
Polyspace does not flag the call to std::forward
if its argument is
reused in a branch that cannot be reached after the call to std::forward
.
For instance, in this code snippet, the branch where the reuse of variable
t
occurs cannot be reached after the code enters the branch where
std::forward
is
used.
template<typename T> void func(T&& t) { T&& p = t; switch(t) { case 0: p = std::forward<T>(t); break; case 1: t--; //t reused break; } }
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Language support library |
Category: Required, Automated |
Version History
Introduced in R2020b