AUTOSAR C++14 Rule A6-2-2
Expression statements shall not be explicit calls to constructors of temporary objects only
Description
Rule Definition
Expression statements shall not be explicit calls to constructors of temporary objects only.
Rationale
Objects that the compiler creates for a short duration, and then deletes, are temporary objects. The compiler might create temporary objects for specific purposes, such as:
Initializing references
Storing values returned by functions
Type casting
Exception handling
Temporary objects are destroyed once the expression that requires their
construction is completely evaluated. For instance, in evaluating the expression
sum = a*b+c
, the compiler creates two temporary objects to store the
results of the multiplication and addition operations. After the expression is evaluated,
both temporary objects are destroyed. Their scope is limited to the expression
statement.
If an expression is an explicit call to a constructor omitting the object name, the compiler creates a temporary object which is immediately destroyed. Such an explicit call to a constructor might indicate that:
You inadvertently omitted the object name.
You expected the unnamed variable to remain in scope up to the end of the declaration block.
Consider this code snippet where a lock_guard
object is
created.
void foo(){ std::mutex mymutex; std::mutex mymutex2; std::lock_guard<std::mutex> lock{mymutex}; std::lock_guard<std::mutex> {mymutex2}; //... }
lock_guard
object named
lock
. The object lock
protects
mymutex
from concurrent access by multiple thread until the end of the
current block. The second declaration attempts a similar protection for
mymutex2
. Because the lock_guard
object in this case
is not named, it is destroyed immediately after the declaration statement. Perhaps
inadvertently, mymutex2
remains unprotected from concurrency
issues.Avoid expression statements that are only an explicit call to a constructor. To implement the Resource Acquisition Is Initialization (RAII) pattern, use named objects.
Polyspace Implementation
Polyspace® flags any expression statement that constructs an unnamed object and does not use it. You can construct unnamed temporary objects when you use the objects within the declaration expression statement. For example, a temporary object that is used as a function return or on the right-hand side of an assignment is compliant with this rule.
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: Statements |
Category: Required, Automated |