是否都是隐式内联的 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(。
相关文章:
- vs 2015 constexpr变量不恒定,但与2019相比还好吗
- constexpr函数中的静态constexpr变量
- 有时可以在 constexpr 上下文中使用非 constexpr 变量?
- 错误:constexpr 变量'struct2Var'必须由常量表达式初始化
- 是否使用静态 constexpr 变量 odr?
- 是否可以将变体的索引作为 constexpr 变量获取?
- 非 constexpr 变量模板的开销是否为零?
- Constexpr变量不是编译时值
- 为什么非成员静态 constexpr 变量不是隐式内联的?
- 使用 constinit 变量初始化 constexpr 变量
- 这是通过初始化 constexpr 变量来标记编译错误的合理跳转
- "static initialization order fiasco"是 constexpr 变量的问题吗?
- lambda 表达式中引用捕获的 constexpr 变量和非显式捕获的 constexpr 变量之间的区别
- is_constant_evaluated() 应该产生 constexpr 变量吗?
- 是否都是隐式内联的 constexpr 变量
- 为全局constexpr变量生成唯一值
- 错误!Constexpr变量必须通过常数表达式constexpr初始化
- 为什么我不能使用 msvc 在模板类中声明静态 constexpr 变量?
- 如果Constexpr - clang vs.GCC,则非constexpr变量
- Clang 在编译时不会计算非 constexpr 变量的 constexpr 函数的值