On this page
  1. A local that does not change
  2. A method that changes nothing
  3. A parameter's copy, in the definition
  4. constexpr: known when it compiles
  5. In a header: inline constexpr
  6. consteval: only when it compiles
  7. constinit: state that starts known
  8. Who checks what

const

What does not change is marked as not changing: a local, a reference, a method, a parameter's copy, a constant. The reader learns at the declaration that the value at the bottom of the function is the one at the top, and the compiler holds the code to it. Where the value is known when the code compiles, it is constexpr, and the compiler works it out.

Google's guide asks for const "whenever it makes sense". This framework makes it a rule without the "whenever": clang-tidy's misc-const-correctness runs over the framework, its tests and its examples, and nothing in them is left that it refuses but two lines that carry a NOLINT with the reason. It is not about bugs, it is about the reader.

A local that does not change#

Compiles; rmp lint refuses it: misc-const-correctness
int score_for(const int coins, const int seconds) {
    int bonus = seconds < 60 ? 100 : 0;
    return coins * 10 + bonus;
}
snippet.cpp:2:5: warning: variable 'bonus' of type 'int' can be declared 'const' [misc-const-correctness]
    2 |     int bonus = seconds < 60 ? 100 : 0;
      |     ^
      |         const
int score_for(const int coins, const int seconds) {
    const int bonus = seconds < 60 ? 100 : 0;
    return coins * 10 + bonus;
}

The same goes for a loop's variable, which is a new local on every turn, and for a reference that is only read through, which is a const &:

Compiles; rmp lint refuses it: misc-const-correctness
int total(const std::vector<int> &scores) {
    int sum = 0;
    for (int score : scores) sum += score;
    return sum;
}
snippet.cpp:3:10: warning: variable 'score' of type 'int' can be declared 'const' [misc-const-correctness]
    3 |     for (int score : scores) sum += score;
      |          ^
      |              const
int total(const std::vector<int> &scores) {
    int sum = 0;
    for (const int score : scores) sum += score;
    return sum;
}

A method that changes nothing#

A method that reads the object and writes nothing is const, so it can be called on a const object and says at the declaration that it is safe to call anywhere. clang-tidy's readability-make-member-function-const refuses one that is not, in the framework and its tests; the examples leave the readability checks out.

Compiles; rmp lint refuses it: readability-make-member-function-const
class Wallet {
public:
    [[nodiscard]] bool can_pay(const int price) { return _coins >= price; }

private:
    int _coins = 0;
};
snippet.cpp:3:24: warning: method 'can_pay' can be made const [readability-make-member-function-const]
    3 |     [[nodiscard]] bool can_pay(const int price) { return _coins >= price; }
      |                        ^
      |                                                 const
class Wallet {
public:
    [[nodiscard]] bool can_pay(const int price) const { return _coins >= price; }

private:
    int _coins = 0;
};

A parameter's copy, in the definition#

A parameter taken by value is a local of the function, and the same rule holds for it: when the function does not change it, it is const. But only where the function is defined. In a declaration that const means nothing to the caller, who hands over a copy either way, and clang-tidy refuses it there:

Compiles; rmp lint refuses it: readability-avoid-const-params-in-decls
void heal(const int amount);
snippet.cpp:1:11: warning: parameter 'amount' is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions [readability-avoid-const-params-in-decls]
    1 | void heal(const int amount);
      |           ^~~~~
class Ship {
public:
    void heal(int amount);

private:
    int _hp = 3;
};

void Ship::heal(const int amount) { _hp += amount; }

The half in the definition has no check: clang-tidy does not look at parameters. It is a question for whoever reviews the change.

A parameter that is expensive to copy and only read is not a copy at all: it is a const reference. clang-tidy refuses the copy, in the framework and in the examples:

Compiles; rmp lint refuses it: performance-unnecessary-value-param
int count_walls(Level level) { return static_cast<int>(level.walls.size()); }
snippet.cpp:1:23: warning: the parameter 'level' of type 'Level' is copied for each invocation but only used as a const reference; consider making it a const reference [performance-unnecessary-value-param]
    1 | int count_walls(Level level) { return static_cast<int>(level.walls.size()); }
      |                       ^
      |                 const  &
int count_walls(const Level &level) { return static_cast<int>(level.walls.size()); }

Text and sequences have a better answer than a const reference: a std::string_view and a std::span.

constexpr: known when it compiles#

A value the code can know when it compiles is constexpr, and a function that can work one out is constexpr too, so it can be used for one. The name says it: a constexpr is CONSTANT_CASE (Naming).

constexpr float GRAVITY = 980.0f;
constexpr int TILE_SIZE = 16;

constexpr int tiles_across(const int width) { return width / TILE_SIZE; }

constexpr int LEVEL_TILES = tiles_across(800);

No tool can say that a value could have been constexpr and is not; clang-tidy has no such check. It is the other question for review โ€” except in a header, where the rule is stricter and is checked.

In a header: inline constexpr#

A constant at namespace scope in a header is inline constexpr. A const or a constexpr there has internal linkage, so every file that includes the header gets a copy of its own; inline makes it one object for the whole program, and constexpr makes it known when the code compiles. The framework's .clang-tidy holds a query of its own for this, custom-rmp-header-constant, which reads every header its lint reaches โ€” the framework's, its tests' and the examples':

Compiles; rmp lint refuses it: custom-rmp-header-constant
#pragma once

namespace game {
constexpr int START_LIVES = 3;
} // namespace game
include/rmp/snippet.h:4:1: warning: a constant in a header is `inline constexpr`: one object for the program, known when it compiles [custom-rmp-header-constant]
    4 | constexpr int START_LIVES = 3;
      | ^
#pragma once

namespace game {
inline constexpr int START_LIVES = 3;
} // namespace game

It runs only with clang-tidy's --experimental-custom-checks, which rmp lint passes; a clang-tidy started without it reads the same file and runs everything else, saying nothing about this one.

consteval: only when it compiles#

A function that must never run while the game does is consteval: every call is worked out by the compiler, and a call it cannot work out does not compile. A collision layer is a bit, and the bit is known when the code is written:

consteval unsigned layer_bit(const int index) { return 1U << index; }

constexpr unsigned PLAYER = layer_bit(0);
constexpr unsigned ENEMY = layer_bit(1);
Does not compile
const int index = GetRandomValue(0, 3);
const unsigned layer = layer_bit(index);
snippet.cpp:2:16: warning: unused variable 'layer' [-Wunused-variable]
    2 | const unsigned layer = layer_bit(index);
      |                ^~~~~
snippet.cpp:2:24: error: call to consteval function 'layer_bit' is not a constant expression
    2 | const unsigned layer = layer_bit(index);
      |                        ^
snippet.cpp:2:34: note: initializer of 'index' is not a constant expression
    2 | const unsigned layer = layer_bit(index);
      |                                  ^
snippet.cpp:1:11: note: declared here
    1 | const int index = GetRandomValue(0, 3);
      |           ^

constinit: state that starts known#

State at file scope that changes while the game runs, and whose first value is known when the code compiles, is constinit: it is set before any code runs, so no other file's start-up can read it before it is ready. The framework's random number generator is held this way, so that its first value is the same every time.

constinit struct {
    unsigned seed = 1;
    int draws = 0;
} generator;

A value only the running game can produce does not compile there:

Does not compile
constinit struct {
    int seed = GetRandomValue(0, 100);
} generator;
snippet.cpp:3:3: error: variable does not have a constant initializer
    3 | } generator;
      |   ^~~~~~~~~
snippet.cpp:1:1: note: required by 'constinit' specifier here
    1 | constinit struct {
      | ^~~~~~~~~
snippet.cpp:2:16: note: non-constexpr function 'GetRandomValue' cannot be used in a constant expression
    2 |     int seed = GetRandomValue(0, 100);
      |                ^
snippet.cpp:3:3: note: in call to '(unnamed struct at snippet.cpp:1:11)()'
    3 | } generator;
      |   ^~~~~~~~~

Both are C++20, and both are in the oldest compiler a game is built with, NetBSD's GCC 10.5. What that compiler does not have is a constexpr std::string or std::vector: a group of file state that holds one cannot be constinit there, and is set at start-up instead.

Who checks what#

RuleCheckWhere
A local, or a reference, that does not change is constmisc-const-correctnessthe framework, its tests, the examples
A method that changes nothing is constreadability-make-member-function-constthe framework and its tests
No const on a by-value parameter in a declarationreadability-avoid-const-params-in-declsthe framework and its tests
A const by-value parameter in the definitionnone: a review question
A read-only parameter that is costly to copy is a const referenceperformance-unnecessary-value-paramthe framework, its tests, the examples
constexpr, consteval, constinit wherever they fitnone: a review question
A namespace constant in a header is inline constexprcustom-rmp-header-constantevery header the lint reaches

Every check here is clang-tidy, with the framework's .clang-tidy, run by rmp lint over src/, tests/ and examples/. A game does not get that file: there, these rules are a recommendation, and nothing holds a game to them unless it switches the framework's clang-tidy on. What checks what has every rule of these pages in one table.