2017-10-11 34 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暗含顯式)。