AutosarC++18_10-A7.1.1

Constexpr or const specifiers shall be used for immutable data declaration

Required inputs: IR

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 [11]: ES.25: Declare objects const or constexpr unless you want to modify its value later on.
  • C++ Core Guidelines [11]: Con.1: By default, make objects immutable.
  • C++ Core Guidelines [11]: 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

ignore_pointer_variables

ignore_pointer_variables : bool = False

Whether variables of pointer type should be ignored.
 

ignore_smart_pointer_variables

ignore_smart_pointer_variables : bool = False

Whether smart pointer variables should be ignored.
 

only_check_unit_locals

only_check_unit_locals : bool = False

Whether only local variables and global static variables should be checked.

Note: this option is automatically activated during single-file analysis.