AutosarC++17_10-A7.1.1¶
Constexpr or const specifiers shall be used for immutable data declaration
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
If data is declared to be const or constexpr then its value can not be changed by mistake. Also, such declaration can offer the compiler optimization opportunities.
Note that the constexpr specifier in an object declaration implies const as well.
Example
//% $Id: A7-1-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
#include <limits>
void Fn() {
const std::int16_t x1 = 5; // Compliant
constexpr std::int16_t x2 = 5; // Compliant
std::int16_t x3 = 5; // Non-compliant - x3 is not modified but not declared as
// constant (const or constexpr)
}
See Also
- C++ Core Guidelines [10]: ES.25: Declare objects const or constexpr unless you want to modify its value later on.
- C++ Core Guidelines [10]: Con.1: By default, make objects immutable.
- C++ Core Guidelines [10]: Con.4: Use const to define objects with values that do not change after construction.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
variable_missing_const |
An immutable variable shall be const/constexpr qualified. |
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
ignore_pointer_variables¶
ignore_pointer_variables : bool = False
ignore_smart_pointer_variables¶
ignore_smart_pointer_variables : bool = False
only_check_unit_locals¶
only_check_unit_locals : bool = False
Note: this option is automatically activated during single-file analysis.