Rule A7-1-2 (required, implementation, automated)

The constexpr specifier shall be used for values that can be determined at compile time.

Rationale

The constexpr specifier declares that it is possible to evaluate the value of the function or variable at compile time, e.g. integral type overflow/underflow, configuration options or some physical constants. The compile-time evaluation can have no side effects so it is more reliable than const expressions. Note that the constexpr specifier in an object declaration implies const, and when used in a function declaration it implies inline. Note also that since 2014 C++ Language Standard constexpr specifier in member function declaration no longer implicitly implies that the member function is const.

Example

//% $Id: A7-1-2.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
std::int32_t Pow1(std::int32_t number)
{
return (number * number);

}

constexpr std::int32_t Pow2(
std::int32_t number) // Possible compile-time computing
// because of constexpr specifier
{
return (number * number);

}
void Fn()
{
constexpr std::int16_t i1 = 20; // Compliant, evaluated at compile-time
const std::int16_t i2 = 20; // Non-compliant, possible run-time evaluation
std::int32_t twoSquare =
Pow1(2); // Non-compliant, possible run-time evaluation
const std::int32_t threeSquare =
Pow1(3); // Non-compliant, possible run-time evaluation
// static_assert(threeSquare == 9, "pow1(3) did not succeed."); // Value
// can not be static_assert-ed
constexpr std::int32_t fiveSquare =
Pow2(5); // Compliant, evaluated at compile time
static_assert(fiveSquare == 25,
"pow2(5) did not succeed."); // Compliant, constexpr
// evaluated at compile
// constexpr std::int32_t int32Max =
// std::numeric_limits<std::int32_t>::max() + 1; //
// Compliant - compilation error due to
// compile-time evaluation (integer overflow)
}
class A
{
public:
static constexpr double pi = 3.14159265; // Compliant - value of PI can be
// determined in compile time

time

// constexpr double e = 2.71828182; // Non-compliant - constexprs need
// to be static members, compilation error

constexpr A() = default; // Compliant

};

See also

C++ Core Guidelines [11]: Con.5: Use constexpr for values that can be computed at compile time.