C++ 通过引用传递数组参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7902433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:40:41  来源:igfitidea点击:

Passing array arguments by reference

c++arrayspass-by-reference

提问by Eddy Pronk

I came across a function with this signature.

我遇到了一个带有这个签名的函数。

void foo(char (&x)[5])
{
}

This is the syntax for passing a fixed size char array by reference.

这是通过引用传递固定大小的字符数组的语法。

The fact that it requires parentheses around &xstrikes me as unusual.

它需要括号的事实让&x我觉得不寻常。

It is probably part of the C++03 standard.

它可能是 C++03 标准的一部分。

What is this form called and can anyone point out a reference to the standard?

这个表格叫什么,谁能指出对标准的引用?

c++decl is not a friend yet:

c++decl 还不是朋友:

$ c++decl 
Type `help' or `?' for help
c++decl> explain void foo(char (&x)[5])
syntax error

采纳答案by newacct

There is nothing unusual or new about the syntax. You see it all the time in C with pointers. []has higher precedence than &, so you need to put it in parentheses if you want to declare a reference to an array. The same thing happens with *(which has the same precedence as &): For example, to declare a pointer to an array of 5 chars in C, you would do char (*x)[5];. Similarly, a pointer to a function that takes and returns an int would be int (*x)(int);(()has same precedence as []). The story is the same with references, except that references are only on C++ and there are some restrictions to what types can be formed from references.

语法没有什么不寻常或新的地方。在 C 语言中,你总是用指针看到它。[]具有比 更高的优先级&,因此如果要声明对数组的引用,则需要将其放在括号中。同样的事情发生在*(与 具有相同的优先级&):例如,要在 C 中声明一个指向 5 个字符的数组的指针,您可以执行char (*x)[5];. 类似地,指向接受并返回 int 的函数的指针将是int (*x)(int);( 与()具有相同的优先级[])。引用的情况也一样,只是引用仅在 C++ 上使用,并且对可以从引用形成的类型有一些限制。

回答by Kerrek SB

There's nothing to explain, this is simply how the parsing rules for declarations work in C++:

没什么好解释的,这就是 C++ 中声明的解析规则是如何工作的:

char  & x[5] // declare x as array 5 of reference to char (not valid C++!)
char (&x)[5] // declare x as reference to array 5 of char

Caution:The first version is not valid C++, though, since you cannot have arrays of references. This is merely an explanation of the declaration syntax. (Sorry about taking so long to get this right, and thanks to the helpful comments!)

注意:第一个版本不是有效的 C++,因为你不能有引用数组。这仅仅是对声明语法的解释。(抱歉花了这么长时间才弄对,感谢有用的评论!)

You're allowed to wrap the type identifier in arbitray levels of parentheses if you like, so you can also say char &(x)[5](first case) or char (((&x)))[5](second case).

如果您愿意,您可以将类型标识符包装在任意级别的括号中,因此您也可以说char &(x)[5](first case) 或char (((&x)))[5](second case)。

回答by nobar

c++declmostly works for this. It's just a bit picky about what you give it.

c++decl主要用于此。只是对你给它的东西有点挑剔。

c++decl> explain void foo(char (&)[5])
declare foo as function (reference to array 5 of char) returning void

c++decl> explain void foo(char &[5])
declare foo as function (array 5 of reference to char) returning void

As noted in another answer, an array of references is illegal. The GNU C++ compiler reports:

如另一个答案所述,引用数组是非法的。GNU C++ 编译器报告:

error: declaration of ‘x' as array of references

错误:将“x”声明为引用数组

By the way, hereis a link to an online utility that serves cdecl(although it complains about references because the version hosted by the site is C-specific).

顺便说一下,这里是一个在线实用程序的链接cdecl(尽管它抱怨引用,因为该站点托管的版本是特定于 C 的)。