Talk

How to declare a constant in C++?

  • In Russian

The amazing thing about C++ is that the simplest things can be extremely confusing and complicated. It seems to be easy to declare a constant.

We need to choose the linkage and storage duration that is suitable for us, to determine the visibility of constant and what translation unit it will be presented in. Then we need to choose an appropriate definition: global variable, local variable, class static data member. Then we need to pick an appropriate combination of static, inline and constexpr specifiers. We may also make the variable a template or a reference. If we are talking about a string, we need to choose the right type: const char*, char[], std::string, std::string_view.

If these things are obvious for you — congratulations! This is impressive. If this information blows your mind, let's deal with this.

The talk assumes an understanding of linkage and storage duration, declaration and definition, and static, inline and constexpr specifiers. These concepts are explained in Mikhail's previous talk "Specifiers, qualifiers and templates" from C++ Russia Piter 2019.

  • #languages

Invited experts

Talks