AUTOSAR C++14 Rule A18-9-2
Forwarding values to other functions shall be done via: (1) std::move if the value is an rvalue reference, (2) std::forward if the value is forwarding reference
Description
Forwarding values to other functions shall be done via: (1) std::move if the value is an rvalue reference, (2) std::forward if the value is forwarding reference.
Rationale
You can pass an object efficiently by:
Using
std::moveto forward anrvaluereference, declared using&&Using
std::forwardto forward a forwarding or universal reference, declared usingT&&orauto&&
Forwarded rvalue references must bind with an
rvalue reference. Forwarding references can bind with either
lvalue or rvalue references. Using
std::move with forwarding references might result in an unexpected
modification of an lvalue. Using std::forward with
rvalue references is possible but it is error-prone and might increase the complexity of
your code.
Polyspace Implementation
Polyspace® flags the use of
std::moveto forward a forwarding reference to a function, including objects of typeauto &&.Polyspace flags the use of
std::forwardto forward anlvalueorrvaluereference to a function.Polyspace does not flag the use of
std::moveorstd::forwardif no forwarding to a function takes place. For instance, in this code snippet, no defect is raised on the use ofstd::movewith forwarding referenceb2and the use ofstd::forwardwith revalue referenceb1.template <typename T1, typename T2> void func(T1& b1, T2&& b2) { const T1& b10 = std::forward<B>(b1); const T2& b20 = std::forward<B>(b2); const T1& b11 = std::move(b1); const T2& b21 = std::move(b2); }
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 |
PQL Name: std.autosar_cpp14.A18_9_2 |
Version History
Introduced in R2020b