[英]Initialization order of static inline member variables in class templates (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來定義ODR?
static constexpr
也將隱式inline
,否則您需要將其標記為inline
template<typename T>
struct SomeClass {
inline static T test; // Now inline
};
struct SomeClass2 {
static constexpr int test = 9; // inline
};
(CFR)。 來自n4606 [depr.static_constexpr]
為了與先前的C ++國際標准兼容,可以在類外部冗余地重新聲明constexpr靜態數據成員而不使用初始化程序。 不推薦使用此用法。
例:
struct A { static constexpr int n = 5; // definition (declaration in C++ 2014) }; const int A::n; // redundant declaration (definition in C++ 2014)
和[dcl.constexpr]
(巴里打敗了我)
使用constexpr說明符聲明的函數或靜態數據成員隱式地是內聯函數或變量(7.1.6)。
來自[dcl.constexpr]:
使用
constexpr
說明符聲明的函數或靜態數據成員隱式地是內聯函數或變量(7.1.6)。
類模板的靜態(非constexpr
)數據成員沒有這樣的隱式inline
。 但是,在C ++ 17中,我們現在可以將變量本身標記為inline
,[dcl.inline]:
帶有
inline
說明符的變量聲明聲明了一個inline
聯變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.