How to declare a constant in C++?

RU / Day 3 / 12:30 / Track 1

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 the appropriate method of determination: macro (it's a joke), enum, global variable, local variable, static field of the class. In a case of variable we need to specify the appropriate specifier (or combination of them): static, extern, inline, constexpr. We may also make the variable a template or link. If we are talking about a line, we need to put 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.

In this talk we will recall all relevant theory: linkage и storage duration, declaration and definition, static, inline, extern и constexpr and their combinations. We will see what consequences have a constant declaring. As a result, we will sum up universal recommendations for all occasions.