std :: array的演绎指南 [英] deduction guides for std::array
问题描述
我遍历了C ++模板独特的书本,我试图了解 std :: array
的演绎指南是如何工作的.关于标准的定义,以下是声明
I go through the book C++ template unique quide and I try to understand how the deduction guides for std::array
works.
Regarding the definition of the standard the following is the declaration
template <class T, class... U>
array(T, U...) -> array<T, 1 + sizeof...(U)>;
例如,如果在主数组中创建为
For example if in main a array created as
std::array a{42,45,77}
演绎如何发生?
谢谢
推荐答案
演绎如何发生?
很简单.
呼叫
std::array a{42,45,77}
匹配
array(T, U...)
具有 T = decltype(42)
和 U ... = decltype(45),decltype(77)
即 T = int
和 U ... = int,int
.
因此 a {42,45,47}
的类型变为 array< T,1 + sizeof ...(U)>
,因此std :: array< int,1 + sizeof ...(int,int)>
,因此 std :: array< int,1 + 2>
就是 std:: array< int,3>
So the type of a{42,45,47}
become array<T, 1 + sizeof...(U)>
, so std::array<int, 1 + sizeof...(int, int)>
, so std::array<int, 1 + 2>
that is std::array<int, 3>
换句话说:提取参数的类型;第一个( T
)用于为类型指定数组(第一个模板参数);其他仅用于计数( sizeof ...(U)
).但是,对于模板的第二个参数,重要的是也要计算第一个参数(类型为 T
的值,因此 1 + sizeof ...(U中的
). 1
)
In other words: are extracted the types of the arguments; the first one (T
) is used to give the type the array (first template parameter); the others are used just to be counted (sizeof...(U)
). But, for the template second parameter, it's important to count also the first argument (of type T
, so the 1
in 1 + sizeof...(U)
).
这篇关于std :: array的演绎指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!