将static const成员重新声明为constexpr会自动将其限定为内联吗?

时间:2017-10-11 06:29:49

标签: c++ c++17 constexpr

考虑static constexpr成员的这个用例:

// smart_enum.h
class smart_enum {
    // Some data, operations, etc.
    // Cannot use constexpr here, smart_enum is incomplete at this point
    static const smart_enum first, second;
};

constexpr smart_enum smart_enum::first = {}, smart_enum::second = {};

firstsecond自动inline变量?或者我是否需要对其进行限定?或者我无法将它们限定为内联,并且需要稍后在某个源文件中提供定义?我一直对从constconstexpr的这种“重新定义”感到困惑,并希望了解更多关于这真正含义的内容。

具体来说,我想知道const声明与constexpr定义之间的互动,以及inline static constexpr android:autoVerify="true"的自动example.com如何发挥作用

1 个答案:

答案 0 :(得分:1)

标准说:

  

[c ++ 17-12.2.3.2-2]在其类定义中声明非内联静态数据成员不是一个定义,除了cv void之外,它可能是一个不完整的类型。

现在,也许你的困惑源于相信这两个表达

static const smart_enum first; // class scope
constexpr smart_enum smart_enum::first = ...; // namespace scope

声明不同的类型。情况并非如此,因为constexpr T的类型仍为const T(事实上,您始终可以将constexpr const T写为同一事物。)

因此,在您的代码中,您首先声明一个名为'first'的不完整类型'const smart_enum',然后您将其定义为“constexpr smart_enum”(或“constexpr 内联const smart_enum”如果我们添加constexpr意味着明确的所有内容。)