数组引用参数有什么用? [英] What is useful about a reference-to-array parameter?

查看:33
本文介绍了数组引用参数有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现了一些这样的代码:

I recently found some code like this:

typedef int TenInts[10];
void foo(TenInts &arr);

你可以在 foo() 的主体中做什么有用的,如果声明是你不能做的:

What can you do in the body of foo() that is useful, that you could not do if the declaration was:

void foo(int *arr);    // or,
void foo(int arr[]);   // or,
void foo(int arr[10]); // ?

我发现了一个问题 如何传递对数组的引用.我想我在问为什么.

I found a question that asks how to pass a reference to an array. I guess I am asking why.

此外,对于何时是指向数组的指针有用吗?"讨论了函数参数,所以我不认为这是一个重复的问题.

Also, only one answer to "When is pointer to array useful?" discussed function parameters, so I don't think this is a duplicate question.

推荐答案

reference-to-array 参数不允许数组类型衰减为指针类型.即确切的数组类型保留在函数内部.(例如,您可以在参数上使用 sizeof arr/sizeof *arr 技巧并获取元素计数).编译器还将执行类型检查以确保数组参数类型与数组参数类型完全相同,即如果参数声明为 10 个整数的数组,则该参数必须是正好为 10 个的数组整数,没有别的.

The reference-to-array parameter does not allow array type to decay to pointer type. i.e. the exact array type remains preserved inside the function. (For example, you can use the sizeof arr / sizeof *arr trick on the parameter and get the element count). The compiler will also perform type checking in order to make sure the array argument type is exactly the same as the array parameter type, i.e. if the parameter is declared as a array of 10 ints, the argument is required to be an array of exactly 10 ints and nothing else.

实际上,在编译时固定数组大小的情况下,可以将使用数组引用(或数组指针)参数声明视为主要的,传递数组的首选方式.另一种变体(当允许数组类型衰减为指针类型时)保留用于需要传递运行时大小的数组的情况.

In fact, in situations when the array size is fixed at compile-time, using a reference-to-array (or pointer-to-array) parameter declarations can be preceived as the primary, preferred way to pass an array. The other variant (when the array type is allowed to decay to pointer type) are reserved for situations when it is necessary to pass arrays of run-time size.

例如,将编译时大小的数组传递给函数的正确方法是

For example, the correct way to pass an array of compile-time size to a function is

void foo(int (&arr)[10]); // reference to an array

void foo(int (*arr)[10]); // pointer to an array

一种可以说是不正确的方法是使用腐朽"的方法

An arguably incorrect way would be to use a "decayed" approach

void foo(int arr[]); // pointer to an element
// Bad practice!!!

衰减"方法通常应保留用于运行时大小的数组,并且通常在单独的参数中伴随数组的实际大小

The "decayed" approach should be normally reserved for arrays of run-time size and is normally accompanied by the actual size of the array in a separate parameter

void foo(int arr[], unsigned n); // pointer to an element
// Passing a run-time sized array

换句话说,当涉及到引用到数组(或指向数组的指针)传递时,真的没有为什么"的问题.如果数组大小在编译时是固定的,则默认情况下,您应该尽可能自然地使用此方法.当您使用数组传递的衰减"方法时,为什么"问题真的会出现.decayed"方法只能用作传递运行时大小的数组的专门技巧.

In other words, there's really no "why" question when it comes to reference-to-array (or pointer-to-array) passing. You are supposed to use this method naturally, by default, whenever you can, if the array size is fixed at compile-time. The "why" question should really arise when you use the "decayed" method of array passing. The "decayed" method is only supposed to be used as a specialized trick for passing arrays of run-time size.

以上基本上是一个更通用的原则的直接结果.当你有一个 T 类型的重"对象时,你通常通过指针 T * 或引用 T & 传递它.数组也不例外.他们没有理由这样做.

The above is basically a direct consequence of a more generic principle. When you have a "heavy" object of type T, you normally pass it either by pointer T * or by reference T &. Arrays are no exception from this general principle. They have no reason to be.

请记住,尽管在实践中编写处理运行时大小的数组的函数通常是有意义的,尤其是在涉及通用库级函数时.这样的功能更加通用.这意味着通常有充分的理由在现实生活中的代码中使用腐朽"的方法,然而,这并不能成为代码作者在编译时识别数组大小并使用引用到的情况的借口-array 方法.

Keep in mind though that in practice it is often makes sense to write functions that work with arrays of run-time size, especially when it comes to generic, library-level functions. Such functions are more versatile. That means that often there's a good reason to use the "decayed" approach in real life code, Nevertheless, this does not excuse the author of the code from recognizing the situations when the array size is known at compile time and using the reference-to-array method accordingly.

这篇关于数组引用参数有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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