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

35,179

Solution 1

You can use a template taking the array size: 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.

Solution 2

Per 3.9.3:2

Any cv-qualifiers applied to an array type affect the array element type, not the array type (8.3.4).

and 8.3.4:1

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

Also, per 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.

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:

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

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

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

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

Solution 3

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);
}

Solution 4

If you need the size of the array:

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

Related videos on Youtube

Kevin MOLCARD
Author by

Kevin MOLCARD

Updated on December 11, 2021

Comments

  • Kevin MOLCARD
    Kevin MOLCARD over 2 years

    I would like to be able to pass a const array argument to a method in 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.

    • Keldon Alleyne
      Keldon Alleyne almost 12 years
    • Mike Seymour
      Mike Seymour almost 12 years
      @avasopht: Because arrays aren't always evil, and dynamic allocation sometimes is evil.
  • twalberg
    twalberg almost 12 years
    While functional, this is not particularly elegant, because, if I call myMethod 100 different times with different sizes, I get 100 different functions. Instant code bloat. Depending on what the function actually does, the optimizer may be able to use a single function instead, but in most useful cases, I don't think that'll be true...
  • Kevin MOLCARD
    Kevin MOLCARD almost 12 years
    @BoBTFish: thanks for this. Do you know if there is a solution to avoid accessing an element outside of the bounds of the array: int x = intArray[N];
  • Kevin MOLCARD
    Kevin MOLCARD almost 12 years
    @twalberg: this is a good point. What would you suggest? Maybe a more generic but lest robust method like the one I wrote in my initial post?
  • twalberg
    twalberg almost 12 years
    @KevinMOLCARD It's always a tradeoff... If I know that I only have a handful of N that I need to instantiate, I might take the route suggested originally in the answer by @BoBTFish. If there's potentially more, I would either take the alternative in his edit (passing the size as an extra argument), or maybe wrap the array and it's size in a small struct and pass a reference/pointer to that instead to avoid having too many parameters. As usual, there's no one-size-fits-all solution.
  • Scott Jones
    Scott Jones about 11 years
    I love this technique, and suggest it to all my junior devs. It's great to be able to capture a static C array (both address and size) and not just a decayed pointer, and worry about the size being out of sync.
  • v.oddou
    v.oddou about 9 years
    You have code bloat if metaprogramming unfolds a lot of N. but if all your hand-made call sites uses different N, your code is already bloated. you're not making it grow so much with a function that's just one line. that's as many as you have call sites.