C++17 中的模板中是否默认内联静态变量?

Are static variables inlined by default inside templates in C++17?

在 C++17 的模板中,静态变量是否默认内联?这是一个例子:

template<typename T>
struct SomeClass {
    static T test;
};

struct SomeClass2 {
    static constexpr int test = 9;
};

这些变量是内联的还是仍需要外联定义才能使用 ODR?

A static constexpr 也将隐含地成为 inline,否则您需要将其标记为 inline

template<typename T>
struct SomeClass {
    inline static T test; // Now inline
};

struct SomeClass2 {
    static constexpr int test = 9; // inline
};

参照来自 n4606 [depr.static_constexpr]

For compatibility with prior C++ International Standards, a constexpr static data member may be redundantly redeclared outside the class with no initializer. This usage is deprecated.

Example:

struct A {
  static constexpr int n = 5; // definition (declaration in C++ 2014)
};
const int A::n; // redundant declaration (definition in C++ 2014)

[dcl.constexpr](巴里先于我)

A function or static data member declared with the constexpr specifier is implicitly an inline function or variable (7.1.6).

来自[dcl.constexpr]:

A function or static data member declared with the constexpr specifier is implicitly an inline function or variable (7.1.6).

class 模板的静态(非constexpr)数据成员没有这样的隐式 inline。但是,在 C++17 中,我们现在可以将变量本身标记为 inline、[dcl.inline]:

A variable declaration with an inline specifier declares an inline variable.