Reputation: 33
template<class T, T i> void f(int[10][i]) { };
int main() {
int a[10][30];
f(a);
}
Why does f(a)
fail?
Upvotes: 3
Views: 1961
Reputation: 392833
template< std::size_t N > void f(int (&arr)[10][N])
{
}
int main() {
int a[10][30];
f(a);
}
This one works (http://codepad.org/iXeqanLJ)
Useful backgrounder: Overload resolution and arrays: which function should be called?
Upvotes: 1
Reputation: 46607
Try this:
template<class T, T i> void f(T[10][i]) { }; // note the 'T'
int main() {
int a[10][30];
f(a);
}
.. this enables the compiler to deduce the type of T
, which is totally impossible in your sample (because T
is not used at all).
Upvotes: 4
Reputation: 92854
f(a)
fails because a template type argument cannot be deduced from the type of a non-type argument. In this case the compiler cannot deduce the type of the template parameter T
.
Try calling it as f<int>(a);
Upvotes: 4