这个函数模板如何推导出数组的大小?
在以下代码中:
#include <iostream>
template <typename T, size_t N>
void cal_size(T (&a)[N])
{
std::cout << "size of array is: " << N << std::endl;
}
int main()
{
int a[] = {1,2,3,4,5,6};
int b[] = {1};
cal_size(a);
cal_size(b);
}
正如预期的那样,两个数组的大小都被打印出来。 N
如何自动初始化为数组大小的正确值(数组通过引用传递)?
In the following code:
#include <iostream>
template <typename T, size_t N>
void cal_size(T (&a)[N])
{
std::cout << "size of array is: " << N << std::endl;
}
int main()
{
int a[] = {1,2,3,4,5,6};
int b[] = {1};
cal_size(a);
cal_size(b);
}
As expected, the size of both the arrays gets printed. How does N
automatically get initialized to the correct value of the array-size (arrays are being passed by reference)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
N
不会“初始化”任何东西。它不是一个变量。它不是一个物体。N
是一个编译时常量。N
仅在编译期间存在。N
的值以及实际的T
由称为“模板参数推导”的过程确定。T
和N
都是根据传递给模板函数的参数的实际类型推导出来的。在第一次调用中,参数类型为
int[6]
,因此编译器推断出T == int
和N == 6
,生成一个单独的函数并调用它。我们将其命名为cal_size_int_6
请注意,此函数中不再有
T
和N
。两者都在编译时被其实际推导值替换。在第一次调用中,参数类型为
int[1]
,因此编译器推断出T == int
和N == 1
,生成也有一个单独的函数并调用它。我们将其命名为cal_size_int_1
此处相同。
您的
main
本质上可以翻译为:换句话说,您的
cal_size
模板产生了两个不同的函数(所谓的专业化原始模板),每个都有不同的N
值(和T
)硬编码到正文中。这就是模板在 C++ 中的工作原理。N
does not get "initialized" to anything. It is not a variable. It is not an object.N
is a compile-time constant.N
only exists during compilation. The value ofN
as well as the actualT
is determined by the process called template argument deduction. BothT
andN
are deduced from the actual type of the argument you pass to your template function.In the first call the argument type is
int[6]
, so the compiler deduces thatT == int
andN == 6
, generates a separate function for that and calls it. Let's name itcal_size_int_6
Note that there's no
T
and noN
in this function anymore. Both were replaced by their actual deduced values at compile time.In the first call the argument type is
int[1]
, so the compiler deduces thatT == int
andN == 1
, generates a separate function for that as well and calls it. Let's name itcal_size_int_1
Same thing here.
Your
main
essentially translates intoIn other words, your
cal_size
template gives birth to two different functions (so called specializations of the original template), each with different values ofN
(andT
) hardcoded into the body. That's how templates work in C++.它之所以有效,是因为
a
的类型是“int
的长度为 6 的数组”,而b
的类型是“长度为 1 的数组”代码>int
”。编译器知道这一点,因此它可以调用正确的函数。特别是,第一个调用调用模板实例cal_size<6>()
,第二个调用调用cal_size<1>()
,因为这些是唯一的模板实例匹配他们各自的论点。如果您尝试调用显式模板实例,则只有尺寸正确时它才会起作用,否则参数将不匹配。考虑以下几点:
It works because the type of
a
is "array of length 6 ofint
" and the type ofb
is "array of length 1 ofint
". The compiler knows this, so it can call the correct function. In particular, the first call calls the template instancecal_size<6>()
and the second call callscal_size<1>()
, since those are the only template instantiations which match their respective arguments.If you attempted to call an explicit template instance, it would only work if you got the size right, otherwise the arguments wouldn't match. Consider the following:
当您声明 int a[] = {1,2,3} 时,它与 int a[3] = {1,2,3} 相同(或将重写为),因为模板化函数以以下形式接收参数T a[N],则 N 的值为 3。
when you declare int a[] = {1,2,3} it is the same as (or will be rewritten as) int a[3] = {1,2,3} since the templated function is receiving argument in form of T a[N], then N will have value of 3.