C++ 向/从函数传递/返回数组(不是指针)的引用的一般规则?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5398930/
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 18:05:11  来源:igfitidea点击:

General rules of passing/returning reference of array (not pointer) to/from a function?

c++arrayscompile-time

提问by Nawaz

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 Mand Nis known at compile time!

在编译时在哪里MN是已知的!

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?

在函数之间传递和返回数组的编译时引用的一般规则是什么?我们如何将类型数组的引用传递int (*a)[M][N]给函数?

EDIT:

编辑:

Adamcommented : int (*a)[N]is not an array, it's a pointer to an array.

亚当评论说:int (*a)[N]不是数组,而是指向数组的指针。

Yes. But one dimension is known at compile time! How can we pass this information which is known at compile time, to a function?

是的。但是一维在编译时是已知的!我们如何将这些在编译时已知的信息传递给函数?

回答by sth

If you want to return a reference to an array from a function, the declaration would look like this:

如果要从函数返回对数组的引用,则声明应如下所示:

// an array
int global[10];

// function returning a reference to an array
int (&f())[10] {
   return global;
}

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:

返回对数组的引用的函数声明看起来与作为数组引用的变量声明相同 - 只是函数名称后跟(),它可能包含参数声明:

int (&variable)[1][2];
int (&functionA())[1][2];
int (&functionB(int param))[1][2];

Such declarations can be made much clearer by using a typedef:

使用 typedef 可以使此类声明更加清晰:

typedef int array_t[10];

array_t& f() {
   return global;
}

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:

如果你想让它变得非常混乱,你可以声明一个函数,它接受对数组的引用并返回这样的引用:

template<int N, int M>
int (&f(int (&param)[M][N]))[M][N] {
   return param;
}

Pointersto arrays work the same, only that they use *instead of &.

指向数组的指针的工作方式相同,只是它们使用*代替&

回答by Thomas Eding

With C++11's trailing return type syntax, you can also write:

使用 C++11 的尾随返回类型语法,您还可以编写:

auto foo () -> int (&)[3]
{
    static int some_array[3]; // doesn't have to be declared here
    return some_array; // return a reference to the array.
}

回答by Erik

You cannot return an array from a function.

您不能从函数返回数组。

8.3.5/6:

8.3.5/6:

Functions shall not have a return type of type array or function, although they may have a return type of type pointer or reference to such things.

函数不应具有数组或函数类型的返回类型,尽管它们可能具有类型指针或对此类事物的引用的返回类型。

EDIT: You'll love the syntax:

编辑:你会喜欢下面的语法:

int (&bar()) [5] {
  static int x[5];
  return x;
}


int (* & bar()) [6][10] {
    static int x[6][10];
    static int (*y)[6][10] = &x;
    return y;
}
// Note - this returns a reference to a pointer to a 2d array, not exactly what you wanted.

回答by Adam Rosenfield

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:

正如Erik 提到的,您不能从函数返回数组。您可以返回一个指针或一个引用,尽管语法相当繁琐:

// foo returns a pointer to an array 10 of int
int (*foo(float arg1, char arg2))[10] { ... }

// bar returns a reference to an array 10 of int
int (&foo(float arg1, char arg2))[10] { ... }

I'd strongly recommend making a typedef for the array type:

我强烈建议为数组类型创建一个 typedef:

// IntArray10 is an alias for "array 10 of int"
typedef int IntArray10[10];

// Equivalent to the preceding definitions
IntArray10 *foo(float arg1, char arg2) { ... }
IntArray10 &bar(float arg1, char arg2) { ... }

回答by Richard Struben

Supplemental to the fine answer by sth, here is how to declare a class with a constant method returning an array reference:

作为 sth 的好答案的补充,这里是如何使用返回数组引用的常量方法声明一个类:

class MyClass
{
public:
    const int (&getIntArray() const)[10];
};

回答by Mark B

This is tagged C++, so I'm going to suggest that the way to return an array in C++ is to return a std::vectorand not try any trickery with C-arrays (which should be used only in carefully selected scenarios in C++ code).

这是标记为 C++,所以我将建议在 C++ 中返回数组的方法是返回 astd::vector而不要尝试使用 C 数组的任何技巧(应该只在 C++ 代码中精心选择的场景中使用)。

As other answers noted, you can't return C-arrays from functions.

正如其他答案所指出的,您不能从函数返回 C 数组。