在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 ?
A static constexpr
将隐式地也为inline
,否则您需要将其标记为inline
template<typename T>
struct SomeClass {
inline static T test; // Now inline
};
struct SomeClass2 {
static constexpr int test = 9; // inline
};
病死率。from 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]
(Barry先我一步)
用constexpr说明符声明的函数或静态数据成员隐式地是内联函数或变量(7.1.6)。
From [dcl.constexpr]:
对于类模板的静态(非用
constexpr
声明的函数或静态数据成员说明符是隐式内联函数或变量(7.1.6)。
constexpr
)数据成员,没有这样的隐式inline
。然而,在c++ 17中,我们现在可以将变量本身标记为inline
, [dcl.inline]:
带有
inline
说明符的变量声明声明了一个内联变量。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 如果全局变量默认是外部变量,为什么要添加"extern"关键字?
- Makefile g++ 使用命令行中的 -D 变量进行编译,默认值
- C++对象默认变量
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- 变量始终在函数中重置为默认值
- 为什么 std::move 不将默认移动构造函数中的源变量更改为默认值?
- 如果输入类型与目标类型不同,"cin"变量是否重置为某个默认值?
- C++中未初始化成员布尔变量的默认值是多少?
- char指针或char变量的默认值是多少
- const变量是否可以在具有默认值的参数中赋值(作为可选参数)
- F.54:如果捕获此变量,请显式捕获所有变量(无默认捕获)
- 有没有更优雅的方法在构造函数中声明默认变量?
- 变量不能在 lambda 中隐式捕获,并且没有使用 switch 语句指定捕获默认值
- make_shared是否对每个成员变量执行默认初始化(零初始化)
- C++什么时候是使用无默认构造函数与getter和setters与直接调用类中的变量的正确时机
- 使用默认构造函数引用成员变量初始化错误
- C++中用于将默认变量类型设置为int的任何选项
- 类中带有g++的默认变量
- LibCinder:着色器参数的默认变量名