# const

> const on everything that does not change, constexpr for what is known when the code compiles, inline constexpr for a constant in a header, consteval and constinit where they fit — each with its reason, and the check that refuses the mistake.

Source: https://omardev29.github.io/rmp-docs/guidelines/const.html

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`

```cpp
int score_for(const int coins, const int seconds) {
    int bonus = seconds < 60 ? 100 : 0;
    return coins * 10 + bonus;
}
```

```cpp
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`

```cpp
int total(const std::vector<int> &scores) {
    int sum = 0;
    for (int score : scores) sum += score;
    return sum;
}
```

```cpp
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`

```cpp
class Wallet {
public:
    [[nodiscard]] bool can_pay(const int price) { return _coins >= price; }

private:
    int _coins = 0;
};
```

```cpp
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`

```cpp
void heal(const int amount);
```

```cpp
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`

```cpp
int count_walls(Level level) { return static_cast<int>(level.walls.size()); }
```

```cpp
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`](https://omardev29.github.io/rmp-docs/guidelines/strings.html) and [a `std::span`](https://omardev29.github.io/rmp-docs/guidelines/pointers-and-raii.html#spans).

## `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](https://omardev29.github.io/rmp-docs/guidelines/naming.html)).

```cpp
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`

```cpp
#pragma once

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

```cpp
#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:

```cpp
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

```cpp
const int index = GetRandomValue(0, 3);
const unsigned layer = layer_bit(index);
```

The compiler refuses it, saying among other things: `constant expression`

## `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.

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

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

Does not compile

```cpp
constinit struct {
    int seed = GetRandomValue(0, 100);
} generator;
```

The compiler refuses it, saying among other things: `constant initializer`

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

| Rule | Check | Where |
|---|---|---|
| A local, or a reference, that does not change is `const` | `misc-const-correctness` | the framework, its tests, the examples |
| A method that changes nothing is `const` | `readability-make-member-function-const` | the framework and its tests |
| No `const` on a by-value parameter in a declaration | `readability-avoid-const-params-in-decls` | the framework and its tests |
| A `const` by-value parameter in the definition | none: a review question |  |
| A read-only parameter that is costly to copy is a `const` reference | `performance-unnecessary-value-param` | the framework, its tests, the examples |
| `constexpr`, `consteval`, `constinit` wherever they fit | none: a review question |  |
| A namespace constant in a header is `inline constexpr` | `custom-rmp-header-constant` | every 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](https://omardev29.github.io/rmp-docs/guidelines/index.html#switching-on-clang-tidy). [What checks what](https://omardev29.github.io/rmp-docs/guidelines/enforcement.html) has every rule of these pages in one table.
