Rule A7-1-1 (required, implementation, automated)
Constexpr or const specifiers shall be used for immutable data declaration.
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.