这个C ++代码是什么意思? [英] What does this C++ code mean?

查看:128
本文介绍了这个C ++代码是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码返回堆栈分配的数组的大小:

The following code returns the size of a stack-allocated array:

template<typename T, int size>
int siz(T (&) [size])
{
    return size;
}

但我无法围绕语法。
特别是 T(&)[size] part ...

but I can't wrap my head around the syntax. Especially the T (&) [size] part...

推荐答案


但是我不能绕开语法。特别是 T(&)[size] part ...

but I can't wrap my head around the syntax. Especially the T (&) [size] part...

该部分是对数组的引用。有用于解密任何C和C ++
声明的右 - 左规则

That part is a reference to an array. There is the "right-left rule" for deciphering any C and C++ declarations.

因为函数模板从提供的函数参数中推导出模板参数类型,所以这个函数模板会推断数组的类型和元素数,并返回计数。

Because function templates deduce template argument types from the supplied function arguments what this function template does is deduce the type and element count of an array and return the count.

函数不能通过值接受数组类型,而只能通过指针或引用。该引用用于避免将数组隐式转换为指向其第一个元素的指针(aka,array decay):

Functions can't accept array types by value, rather only by pointer or reference. The reference is used to avoid the implicit conversion of an array to the pointer to its first element (aka, array decay):

void foo(int*);

int x[10];
int* p = x; // array decay
foo(x);     // array decay again

数组衰减会破坏数组的原始类型,丢失。

Array decay destroys the original type of the array and hence the size of it gets lost.

注意,因为它是一个在C ++ 03中的函数调用,返回值不是编译时常量(即返回值不能使用作为模板参数)。在C ++ 11中,函数可以用 constexpr 标记以返回编译时常量:

Note, that because it is a function call in C++03 the return value is not a compile time constant (i.e. the return value can't be used as a template argument). In C++11 the function can be marked with constexpr to return a compile time constant:

template<typename T, size_t size>
constexpr size_t siz(T(&)[size]) { return size; }

要获取数组元素计数作为C ++ 03中的编译时常数,可以使用:

To get the array element count as a compile time constant in C++03 a slightly different form may be used:

template<class T, size_t size>
char(&siz(T(&)[size]))[size]; // no definition required

int main()
{
    int x[10];
    cout << sizeof siz(x) << '\n';
    double y[sizeof siz(x)]; // use as a compile time constant 10
}

在上面它声明了一个函数模板具有相同的引用到数组参数,但返回值类型为 char(&)[size] 规则)。注意,函数调用永远不会在运行时发生,这就是为什么不必定义函数模板 siz 的原因。 sizeof siz(x)基本上是说如果 siz(x)

In the above it declares a function template with the same reference-to-an-array argument, but with the return value type of char(&)[size] (this is where the "right-left rule" can be appreciated). Note that the function call never happens at run-time, this is why the definition of function template siz is unnecessary. sizeof siz(x) is basically saying "what would be the size of the return value if siz(x) were called".

以数组的元素数作为编译时常数的旧C / C ++方法是:

The old C/C++ way of getting the element count of an array as a compile time constant is:

#define SIZ(arr) (sizeof(arr) / sizeof(*(arr)))

这篇关于这个C ++代码是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆