簡體   English   中英

所有的 constexpr 變量都是隱式內聯的嗎?

[英]Are all constexpr variable implicitly inline?

我在玩自動模板參數,我很驚訝這段代碼沒有編譯:

constexpr auto bar = 2;

template<auto& T>
struct Foo {
    auto operator()() const { return T; }
};

int main() {
    Foo<bar> b;
    b();
}

Visual Studio 15.7(預覽版 4)吐出這些錯誤:

 error C2970: 'Foo': template parameter 'T': 'bar': an expression involving objects with internal linkage cannot be used as a non-type argument note: see declaration of 'Foo' note: see declaration of 'bar' error C2440: 'specialization': cannot convert from 'int' to 'int &' note: see reference to class template instantiation 'Foo<0>' being compiled error C2973: 'Foo': invalid template argument 'int' note: see declaration of 'Foo'

然后,在添加inline ,錯誤消失了!

constexpr inline auto bar = 2;

我認為constexpr變量是隱式inline 另外,這如何影響我的變量bar的鏈接?

所有的 constexpr 變量都是隱式內聯的嗎?

不可以。只有 constexpr 函數和 constexpr 靜態數據成員是隱式內聯的 ( [dcl.constexpr]/1 )。

另外,這如何影響我的變量欄的鏈接?

constexpr 變量是const ( [dcl.constexpr]/9 )。 未顯式聲明為extern的非內聯const變量具有內部鏈接 ( [basic.link]/3 )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM