AUTOSAR C++14 Rule A12-8-7
Description
Rule Definition
Assignment operators should be declared with the ref-qualifier &.
Rationale
You can use ref-qualifiers to specify whether a function or operator applies to lvalues
or rvalues. Functions or operators that apply to lvalues have the ref-qualifier
&
. Functions and operators that apply on rvalues have the
ref-qualifier &&
at the end of their declaration.
Built-in assignment operators in C++ accept only lvalues as input parameters. If
user-defined assignment operators take both rvalue and lvalue as input parameters, it can
cause confusion and errors. Consider this code where the user-defined assignment operator
for the class obj
accepts both rvalues and lvalues as input parameters.
class obj{ obj& operator=(Obj const&){ //... return *this; } //... }; int main(){ int i,j,k; obj a,b,c; if((i+j)=k) // compilation error //... if((a+b)=c) // silent error //... }
In the first
if
statement, the equal-to operator (==
) is written as an assignment operator (=
) because of a typographical error. Because the built-in assignment operator forint
does not accept rvalues as input, the statement(i+j) = k
causes a compilation error.The condition for the second
if
statement contains a similar error. Because the user-defined assignment operator for classobj
accepts both lvalues and rvalues as input, the statement(a+b) = c
compiles without error. Theif
block executes unexpectedly, resulting in a silent bug.
To avoid errors and confusion, specify that assignment operators take only lvalues as
input parameters by adding the ref-qualifier &
to their
declaration.
Polyspace Implementation
Polyspace® flags user-defined assignment, compound assignment, increment, and decrement operators when:
They do not have the ref-qualifier
&
in their declaration.They are member functions of a class.
They are not declared as
= delete
.
Because ref-qualifiers are applicable only to nonstatic member functions, this rule does not apply to nonmember assignment operators.
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: Special member functions |
Category: Advisory, Automated |
Version History
Introduced in R2020b