繁体   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