向函数传递/返回数组(而不是指针)引用的一般规则?
我们可以将数组的引用传递给函数,例如:
void f(int (&a)[5]);
int x[5];
f(x); //okay
int y[6];
f(y); //error - type of y is not `int (&)[5]`.
或者更好的是,我们可以编写函数模板:
template<size_t N>
void f(int (&a)[N]); //N is size of the array!
int x[5];
f(x); //okay - N becomes 5
int y[6];
f(y); //okay - N becomes 6
现在我的问题是,如何从函数返回数组的引用?
我想从函数返回以下类型的数组:
int a[N];
int a[M][N];
int (*a)[N];
int (*a)[M][N];
其中 M
和 N
在编译时已知!
将数组的编译时引用传递给函数或从函数返回的一般规则是什么?我们如何将 int (*a)[M][N]
类型的数组的引用传递给函数?
编辑:
Adam评论:int (*a)[N]
不是一个数组,它是一个指向数组的指针。
是的。但一维在编译时是已知的!我们如何将编译时已知的信息传递给函数?
We can pass reference of an array to a function like:
void f(int (&a)[5]);
int x[5];
f(x); //okay
int y[6];
f(y); //error - type of y is not `int (&)[5]`.
Or even better, we can write a function template:
template<size_t N>
void f(int (&a)[N]); //N is size of the array!
int x[5];
f(x); //okay - N becomes 5
int y[6];
f(y); //okay - N becomes 6
Now my question is, how to return reference of an array from a function?
I want to return array of folllowing types from a function:
int a[N];
int a[M][N];
int (*a)[N];
int (*a)[M][N];
where M
and N
is known at compile time!
What are general rules for passing and returning compile-time reference of an array to and from a function? How can we pass reference of an array of type int (*a)[M][N]
to a function?
EDIT:
Adam commented : int (*a)[N]
is not an array, it's a pointer to an array.
Yes. But one dimension is known at compile time! How can we pass this information which is known at compile time, to a function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果要从函数返回对数组的引用,则声明将如下所示:
返回数组引用的函数的声明看起来与作为数组引用的变量的声明相同 - 只是函数名后面跟着
()
,它可能包含参数声明:使用 typedef 可以使此类声明更加清晰:
如果你想让它变得非常混乱,你可以声明一个函数获取对数组的引用并返回这样的引用:
指向数组的指针的工作方式相同,只是它们使用
*
而不是&
。If you want to return a reference to an array from a function, the declaration would look like this:
The declaration of a function returning a reference to an array looks the same as the declaration of a variable that is a reference to an array - only that the function name is followed by
()
, which may contain parameter declarations:Such declarations can be made much clearer by using a typedef:
If you want it to get really confusing, you can declare a function that takes a reference to an array and also returns such a reference:
Pointers to arrays work the same, only that they use
*
instead of&
.使用 C++11 的尾随返回类型语法,您还可以编写:
With C++11's trailing return type syntax, you can also write:
您不能从函数返回数组。
8.3.5/6:
编辑:你会喜欢语法:
You cannot return an array from a function.
8.3.5/6:
EDIT: You'll love the syntax:
正如埃里克提到,你不能从函数返回数组。您可以返回指针或引用,尽管语法非常复杂:
我强烈建议为数组类型创建 typedef:
As Erik mentioned, you can't return an array from a function. You can return a pointer or a reference, although the syntax is quite hairy:
I'd strongly recommend making a typedef for the array type:
作为对 sth 的良好答案的补充,以下是如何使用返回数组引用的常量方法声明一个类:
Supplemental to the fine answer by sth, here is how to declare a class with a constant method returning an array reference:
这是标记为 C++ 的,所以我建议在 C++ 中返回数组的方法是返回 std::vector ,而不是尝试使用 C 数组进行任何欺骗(应该使用仅在 C++ 代码中精心选择的场景中)。
正如其他答案所述,您不能从函数返回 C 数组。
This is tagged C++, so I'm going to suggest that the way to return an array in C++ is to return a
std::vector
and not try any trickery with C-arrays (which should be used only in carefully selected scenarios in C++ code).As other answers noted, you can't return C-arrays from functions.