这个“数组大小"是如何计算的?模板功能有效吗? [英] How does this "size of array" template function work?
问题描述
有人可以解释一下这段代码是如何工作的吗?我知道这段代码的目的是获取数组的长度,但我不知道这段代码是如何工作的:
Can someone explain how this code works? I know that the purpose of this code is to get the length of an array, but I don't know how this code works:
template<typename T, int size>
int GetArrLength(T(&)[size]){return size;}
推荐答案
首先让我们剖析一下参数,T(&)[size]
.从内到外,从右到左,先读括号组的声明:这是一个未命名的参数,它是对 T
类型的大小为 size
的数组的引用.
First let's dissect the parameter, T(&)[size]
. Read declarations from inside out, right to left, parenthesis group first: It's an unnamed parameter that is a reference to an array of size size
of type T
.
也就是说,它接受对任何数组的引用,其中数组的类型和大小是模板参数.
That is, it accepts a reference to any array, where the type and size of the array are template parameters.
如果我们这样称呼它:
int a[10];
GetArrLength(a);
编译器会尝试推断模板参数.要使参数类型与您传递的内容相匹配,T
必须是 int
并且 size
必须是 10(使参数成为对10 个 int
的数组).
The compiler will try to deduce the template parameters. For the parameter type to match what you're passing, T
must be int
and size
must be 10 (making the parameter a reference to an array of 10 int
s).
然后返回该大小,给出数组中元素的数量.
You then return that size, giving you the number of elements in an array.
这段代码有两个问题".首先,大小不能为负,因此使用带符号类型作为模板参数和返回类型是没有意义的.相反,应该使用无符号类型;最好是 std::size_t
:
There are two "problems" with this code. Firstly, sizes cannot be negative, so it doesn't make sense to use a signed type as the template parameter and return type. Rather, an unsigned type should be used; best would be std::size_t
:
template<typename T, std::size_t Size>
std::size_t GetArrLength(T(&)[Size]) { return size; }
第二个是这个函数的结果不是一个常量表达式,即使数组的大小是.虽然在大多数情况下这很好,但如果我们能从中得到一个常量表达式会更好.这就是您最终获得此解决方案的地方:
The second is that the result of this function is not a constant-expression, even though an array's size is. While that's fine in most situations, it would be better if we could get a constant-expression from it. That's where you end up with this solution:
template <std::size_t N>
struct type_of_size
{
typedef char type[N];
};
template <typename T, std::size_t Size>
typename type_of_size<Size>::type& sizeof_array_helper(T(&)[Size]);
#define sizeof_array(pArray) sizeof(sizeof_array_helper(pArray))
这是这样使用的:
int a[10];
const std::size_t n = sizeof_array(a); // constant-expression!
它通过三件事起作用:第一件事与上面的想法相同,模板参数将被填写给你数组的大小.
It works by three things: the first is the same idea as above, that template parameters will be filled out giving you the array's size.
第二部分是使用该信息来创建具有特定大小的类型,因此是 type_of_size
助手.这部分不是绝对必要的,但我认为它使代码更易于阅读.char[N]
的大小总是等于 N
,因此我们可以滥用它来存储"数组的大小......输入本身!
The second part is using that information to make a type with a specific size, hence the type_of_size
helper. That part isn't strictly necessary, but I think it makes the code easier to read. A char[N]
has a size equal to N
, always, hence we can abuse that to "store" the size of the array...in the size of a type itself!
第三部分是使用 sizeof
获取该大小.它实际上不评估任何东西,所以我们不需要函数的定义.它只是说如果你这样做......大小会......".大小是我们存储"的大小,在 char
数组中.
The third part is getting that size with sizeof
. It doesn't actually evaluate anything, so we don't need a definition for the function. It simply says "If you were to do this...the size would be...". And the size is our "stored" size, in the char
array.
这篇关于这个“数组大小"是如何计算的?模板功能有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!