irep
irep

Reputation: 23

Passing two-dimensional arrays of varying size in c++

I'm trying to call a function with a two-dimensional array of variable size as a parameter. In order to do this, I use a template:

template <int N, int M>
void move(int (&arr)[M][N]);

Now this works fine when I declare the size of the array directly, e.g.:

int arr[5][7];
move(arr);

But if I get the dimensions in from somewhere else, e.g.:

int x, y;
x = 7;
y = 5;
int arr[y][x];
move(arr);

The compiler shows this error:

no matching function for call to 'move(int[((unsigned int)((int)y))][((unsigned int)((int)x))])

... and I don't get it. I'm fairly new to c++ and this may just be me being stupid, and I know multidimensional arrays in c++ are... interesting, but in this case I don't even understand what the problem is. Anyone have an idea?

Edit: I worded the question weirdly, sorry about that - the array itself does not vary its size, but the function should be able to work with arrays of different sizes. The question is why that works when I declare the array's size directly, but not when I declare to parameters for each dimension and then declare the array with those.

Upvotes: 2

Views: 440

Answers (2)

Omnifarious
Omnifarious

Reputation: 56048

Well declaring an array with variable bounds like this is an extension in the first place. It's not actually valid standard C++. Secondly, using bare arrays in C++ is frowned upon. You should be using ::std::vector instead.

The compiler error message is really quite inscrutable. And I'm amused it was able to give you one at all instead of faulting. You are presenting it with an extremely strange case.

Templates parameters have to be compile time constants. They cannot be things whose values might change at runtime. It has to be possible for the compiler to know that the parameters are constant at compile time. This will only happen if the values are actual literal values or named values declared as a constexpr or a simple expression involving literals or values declared as constexpr.

In the case of your array with variables bounds... Neither of those variables are constexprs, so they could potentially change at runtime. The compiler has no way to know in advance which version of the function to generate.

Upvotes: 2

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385164

Because, although you give statically-known values to x and y, these are both mutable variables and, as such, they still don't fit the criteria for array dimensions. The template function then cannot be instantiated with that oddness.

const unsigned int x = 7, y = 5;

This would be enough.

Initialising a const unsigned int with anything other than statically-known quantities (e.g. literals, as above) would not be.

constexpr makes array dimension safety easier to guarantee in C++11.

Upvotes: 1

Related Questions