I wrote the following code for which template argument deduction fails :
template<int>
struct num {};
template<int m>
void match(num<2*m>) {
}
int main()
{
match(num<2>());
return 0;
}
I know from a gut feeling that the compiler can’t deduce the correct m, but I want to understand the theoretical underpinnings for why it fails. Can someone elucidate?
Well, you are basically asking the compiler to solve the equation
2 * m == 2for you in order to determine template argumentmformatch. Compiler does not solve equations during template argument deduction, regardless of how simple and unambiguous they are.The language specification in 14.8.2.4/14 (C++03), 14.8.2.5/16 (C++11) covers your situation and has a similar example
As to why it is done that way… I think it is pretty obvious that in general case the problem of solving a mathematical equation is too complicated. It can also lead to ambiguous solutions or to solutions that don’t belong to the expected domain. For example, what would you expect the compiler to deduce for
match(num<3>())?