| 1 |
.. title:: clang-tidy - cert-dcl21-cpp |
| 2 |
|
| 3 |
cert-dcl21-cpp |
| 4 |
============== |
| 5 |
|
| 6 |
This check flags postfix ``operator++`` and ``operator--`` declarations |
| 7 |
if the return type is not a const object. This also warns if the return type |
| 8 |
is a reference type. |
| 9 |
|
| 10 |
The object returned by a postfix increment or decrement operator is supposed |
| 11 |
to be a snapshot of the object's value prior to modification. With such an |
| 12 |
implementation, any modifications made to the resulting object from calling |
| 13 |
operator++(int) would be modifying a temporary object. Thus, such an |
| 14 |
implementation of a postfix increment or decrement operator should instead |
| 15 |
return a const object, prohibiting accidental mutation of a temporary object. |
| 16 |
Similarly, it is unexpected for the postfix operator to return a reference to |
| 17 |
its previous state, and any subsequent modifications would be operating on a |
| 18 |
stale object. |
| 19 |
|
| 20 |
This check corresponds to the CERT C++ Coding Standard recommendation |
| 21 |
DCL21-CPP. Overloaded postfix increment and decrement operators should return a |
| 22 |
const object. However, all of the CERT recommendations have been removed from |
| 23 |
public view, and so their justification for the behavior of this check requires |
| 24 |
an account on their wiki to view. |