Inline Variables: Explained
Inline variables are a feature introduced in C 17 that allows you to define external linkage, const namespace scope variables or static class data members in a header file. This overcomes the previous limitation of declaring such variables with the inline keyword.
How Inline Variables Work
Inline variables allow you to specify a variable within a class or namespace that has external linkage, meaning it can be defined multiple times in different translation units. The compiler ensures that only one of these definitions is used, resolving the issue of multiple definitions when including the header in multiple units.
Declaring and Using Inline Variables
Inline variables are declared using the inline specifier followed by the type and name of the variable. They are typically defined in header files.
For example:
struct Kath { static inline std::string const hi = "Zzzzz..."; };
This declares a static, inline variable hi of type std::string const within the Kath struct. Since it is marked as inline, it can be defined in a header file and included in multiple translation units without conflicting definitions.
Benefits of Inline Variables
Inline variables offer the following benefits:
The above is the detailed content of What are C 17 Inline Variables and How Do They Work?. For more information, please follow other related articles on the PHP Chinese website!