vittorio
vittorio

Reputation: 33

no matching function for call to function template

template<class T, T i> void f(int[10][i]) { };

int main() {
   int a[10][30];
   f(a);
}

Why does f(a) fail?

http://ideone.com/Rkc1Z

Upvotes: 3

Views: 1961

Answers (3)

sehe
sehe

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

Alexander Gessler
Alexander Gessler

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).

http://ideone.com/gyQqI

Upvotes: 4

Prasoon Saurav
Prasoon Saurav

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

Related Questions