我试图通过以下示例了解变量模板的工作原理:
#include <iostream>
template <class T, const T& t>
int var = t.a;
struct T
{
int a;
constexpr T(): a(31){ }
};
T y;
const T t = y;
const T tt = T();
int main()
{
std::cout << "var <T, t> = " << var<T, t> << std::endl; //0
std::cout << "y.a = " << y.a << std::endl; //31
std::cout <<"var <T, tt> = " << var<T, tt> << std::endl; //31
}
老实说,我真的不知道这种行为。让我感到困惑的是特化 var<T, t>
为 0,但 y.a
是 31
.另外,如果我们初始化 T
类型的对象与临时我们也有不同的结果。你能澄清一下吗?
我的意思是,我正在寻找工作草案中的规范引用 N4296
,描述该行为。
最佳答案
目前,变量模板的指定程度相当低。如果我们通过 the current core issues list ,我们看到了
- It is unclear whether and how a variable template can be defined multiple times in a program.
- It is unclear how definitions and declarations of variable templates are matched.
- It is unclear how variable template partial specializations are supposed to work.
- It is unclear what the point of instantiation of a variable template specialization is.
过去也不清楚初始化顺序变量模板遵循什么。 CWG issue 1744修改 [basic.start.init]/p2 以澄清这一点
Dynamic initialization of a non-local variable with static storage duration is unordered if the variable is an implicitly or explicitly instantiated specialization, and otherwise is ordered [Note: an explicitly specialized static data member or variable template specialization has ordered initialization. —end note].
var<T, t>
是具有静态存储持续时间的非局部变量,是隐式实例化的特化。因此它的动态初始化是无序的。从 t
不符合常量初始化的条件,这意味着 var<T, t>
可能在t
的动态初始化之前初始化, 结果为 0,无论 var
之间的相对顺序如何的定义和t
的定义,并且无论 var<T, t>
的实例化点如何.
因此,moving the definition of var
below the definition of t
和/或 an explicit instantiation of var<T, t>
对正在打印的内容没有影响,而 providing an explicit specialization for var<T, t>
still initializing it to t.a
导致第一行打印 31
.
关于c++ - 通过示例了解变量模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28415414/