Say we need a class member variable (non-const). Global variables should be best avoided, but let’s ignore that for now and focus on the solution.
This is a bad choice most often:
The definition (
int S::i = 0;) needs to be placed in a single translation unit in the whole project, otherwise we get an error indicating multiple definition of
In pre-C++17 world we need to find some workaround of this problem. One way is to go with a static member function:
Another way is to use templates:
That makes a lot of boilerplate and obscure code. Our problem is solved by inline variables in C++17. That looks simple and serves well:
The inline variable has an external linkage if our class is not inside an unnamed namespace (i.e. when the class has external linkage too). It enables the possibility of more than one definition of the variable (hence no multiple definition errors). The variable must be declared inline in all the translation units and it always has the same memory address. All the definitions have to be identical, otherwise we break one definition rule. That may lead to hard to detect problems if we are not cautious.
The code in the example above is invalid, but it compiles and can behave unexpectedly.
Static constexpr members are inline by default.
Interesting resources (click to expand)
Comments on dev.to.