I tried to use tempaltes as array dimension value. I was puzzled when tried to specify wrong dimension as tempate argument. For example code:
#include <iostream>
using namespace std;
template <int X, int Y>
void f(int a[X][Y]) {
for (int i = 0; i < X; ++i) {
for (int j = 0; j < Y; ++j) {
cout << a[i][j] << " ";
}
cout << '\n';
}
}
int main() {
int a[2][2] = {{1, 2}, {3, 4}};
f<2, 2>(a); // compilation succeeded
f<10, 2>(a); // compilation succeeded
f<2, 10>(a); // compilation FAILED
}
Why in the last case compilation fails, but in case <10, 2> it does not?
error: no matching function for call to 'f'
note: candidate function template not viable: no known conversion from 'int [2][2]' to 'int (*)[10]' for 1st argument