将数组作为 C++ 中方法的 const 参数传递

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

passing an array as a const argument of a method in C++

c++arraysmethodsargumentsconst

提问by Kevin MOLCARD

I would like to be able to pass a const array argument to a method in C++.

我希望能够将 const 数组参数传递给 C++ 中的方法。

I know that when you pass an array to method it is the same than passing a pointer to the first item of the array so an easy way is to use the pointer.

我知道当您将数组传递给方法时,它与传递指向数组第一项的指针相同,因此一个简单的方法是使用指针。

void myMethod(int * const inTab)

But having an array is sometimes better, you can write the size of the array for instance.

但是有一个数组有时更好,例如你可以写出数组的大小。

回答by BoBTFish

You can use a template taking the array size: http://ideone.com/0Qhra

您可以使用采用数组大小​​的模板:http: //ideone.com/0Qhra

template <size_t N>
void myMethod ( const int (& intArray) [N] )
{
    std::cout << "Array of " << N << " ints\n";
    return;
}

EDIT: A possible way to avoid code bloat would be to have a function that takes a pointer and a size that does the actual work:

编辑:避免代码膨胀的一种可能方法是使用一个带有指针和大小的函数来完成实际工作:

void myMethodImpl ( const int * intArray, size_t n );

and a trivial template that calls it, that will easily be inlined.

和一个调用它的简单模板,它很容易被内联。

template <size_t N>
void myMethod ( const int (& intArray) [N] )
    { myMethodImpl ( intArray, N ); }

Of course, you'ld have to find a way to test that this is always inlined away, but you do get the safety and ease of use. Even in the cases it is not, you get the benefits for relatively small cost.

当然,您必须找到一种方法来测试它是否始终内联,但您确实获得了安全性和易用性。即使在这种情况下,您也能以相对较低的成本获得好处。

回答by ecatmur

Per 3.9.3:2

每 3.9.3:2

Any cv-quali?ers applied to an array type a?ect the array element type, not the array type (8.3.4).

任何应用于数组类型的 cv 限定符都会影响数组元素类型,而不是数组类型 (8.3.4)。

and 8.3.4:1

和 8.3.4:1

Any type of the form “cv-quali?er-seq array of N T” is adjusted to “array of N cv-quali?er-seq T”, and similarly for “array of unknown bound of T”.

任何类型的“cv-quali?er-seq array of NT”都被调整为“array of N cv-quali?er-seq T”,“array of unknown bound of T”也类似。

Also, per 8.3.5:5

此外,根据 8.3.5:5

After determining the type of each parameter, any parameter of type “array of T” or “function returning T” is adjusted to be “pointer to T” or “pointer to function returning T,” respectively.

在确定每个参数的类型后,任何类型为“T 的数组”或“返回 T 的函数”类型的参数分别调整为“指向 T 的指针”或“指向返回 T 的函数的指针”。

That means that within a function taking an array parameter, the parameter type is actually a pointer, and because of 3.9.3:2 the pointer is non-cv-qualified:

这意味着在采用数组参数的函数中,参数类型实际上是一个指针,并且由于 3.9.3:2 指针是非 cv 限定的:

void foo(const int parameter[10]) {
    parameter = nullptr;   // this compiles!
}

This does not affect the type of the function itself, because of another clause in 8.3.5:5

这不会影响函数本身的类型,因为 8.3.5:5 中的另一个子句

After producing the list of parameter types, any top-level cv-quali?ers modifying a parameter type are deleted when forming the function type.

生成参数类型列表后,在形成函数类型时删除任何修改参数类型的顶级 cv 限定符。

Thus if you want to be able to pass an array with cv qualifiers, it mustbe by reference:

因此,如果您希望能够通过 cv 限定符传递数组,则必须通过引用:

void foo(const int (&parameter)[10]);

回答by Desmond Hume

Not sure if it's what you asked about, but maybe it's what you were looking for

不确定这是否是您问的问题,但也许这就是您要找的

void func (const int array[10])
{
    //array[0] = 12345; // this wouldn't compile, so 'const' works
}

int main ()
{
    int array[10];
    func(array);
}

回答by Torsten Robitzki

If you need the size of the array:

如果您需要数组的大小:

template < std::size_t Size >
void myMethod( const int ( &inTab )[ Size ] );