2017-10-11 35 views
0

考虑一个static constexpr成员的这种使用情况:将静态const成员重新声明为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定义,以及如何与一个static constexprRedefinitions of constexpr static data members are allowed now? (but not inline const)?

+0

“此外,constexpr说明符暗示静态数据成员以及函数的内联。” - 但是我想把这个constexpr放在课堂上。 – lorro

+0

@lorro,但你不能把它放在课堂上,这是我的困惑所在。 – user975989

回答

1

标准的自动inline发挥出来的相互作用说:

[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来表示相同的内容)。

所以,在你的代码,你首先声明一个名为“第一”不完全类型的“常量smart_enum”,那么你福斯产品定义为“constexpr smart_enum”(或“constexpr 内联常量 smart_enum”如果我们添加的一切constexpr暗含显式)。