3
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

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

Mmm suboptimal. It took some tinkering (i.e. reading up), but there is a good way to do this. (The second template param actually should never have been of type T)
Why a downvote? The question asked is why f(a) fails and my post manages to answer that.
4

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

Comments

1
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.