AutosarC++17_10-A13.2.1¶
An assignment operator shall return a reference to “this”
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
Returning a type “T&” from an assignment operator is consistent with the C++ Standard Library.
Example
// $Id: A13-2-1.cpp 271687 2017-03-23 08:57:35Z piotr.tanski $
class A {
public:
// ...
A &operator=(const A &) & // Compliant
{
// ...
return *this;
}
};
class B {
public:
// ...
const B &operator=(const B &) & // Non-compliant - violating consistency
// with standard types
{
// ...
return *this;
}
};
class C {
public:
// ...
C operator=(const C &) & // Non-compliant
{
// ...
return *this;
}
};
class D {
public:
// ...
D *operator=(const D &) & // Non-compliant
{
// ...
return this;
}
};
See Also
- HIC++ v4.0 [8]: 13.2.2 Ensure that the return type of an overloaded binary operator matches the built-in counterparts.
- C++ Core Guidelines [10]: F.47: Return T& from assignment operators.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
asgn_ref_this |
An assignment operator shall return a reference to “this”. |
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
This rule has no individual options.