Expensive post-increment operation
Description
This defect is raised when you use the post-increment or post-decrement operation instead of a more efficient pre-increment or pre-decrement operation. The pre-increment operation is equally or more efficient when all of these conditions are true:
Pre and post increment or decrement operations are defined for the object.
The return type of post-increment or decrement operation is expensive to copy.
The returned value is unused.
The return type of pre-increment or decrement operation is not expensive to copy, such as a reference.
The efficiencies of pre-increment and pre-decrement operations depends on the version and implementation of the C++ standard that you use. If you switch C++ version or the library implementation, you might see a change in the number of violation of this check.
Risk
Post-increment or decrement operations create a copy of the object, increment or decrement the original, and then return the copied object. When the object is expensive to copy and you do not use the returned copy of the object, the post-increment or post-decrement operation is inefficient. Use a pre-increment or pre-decrement operation, which does not copy the object and typically returns a reference to the incremented or decrement object. Inadvertently using post-increment instead of pre-increment with a large object might make the code inefficient. Code that uses inefficient post-increment or post-decrement operations compiles and behaves correctly. The inefficient operations might remain undetected.
Fix
When you do not use the returned object from a decrement or increment operation, use a pre-increment operation.
When iterating over each element of a container, you might want to use loops that do not
require increment or decrement operations, such as std::for_each
or the
range-based for
loop. These loops are optimized for such iterations. They
do not require manual increment or decrement operations.
Performance improvements might vary based on the compiler, library implementation, and environment that you are using.
Examples
Result Information
Group: Performance |
Language: C++ |
Default: Off |
Command-Line Syntax:
EXPENSIVE_POST_INCREMENT |
Impact: Low |
Version History
Introduced in R2021b
See Also
Find defects
(-checkers)
| C++ standard version
(-cpp-version)
| Compiler (-compiler)
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)