I think you wanted the second template to be invoked whenever T
is int
. John has shown you how to do that, and Benoit has shown you what you need to do in order to actually call the second function.
Your problem is that by trying to specialize test<>()
for a specific type (int
) using the completely wrong syntax, you have accidentally hit another valid syntactic form. (Kind of bad luck.) That second function template is using a so-called non-type template parameter. For besides types you can use other things as template parameters. Among others (functions, templates) you can also use integral constants, like int
. Had you tried to do this with, say, double
, the code would have failed to compile.
Your second test<>()
template is an overload of the first one which can be used with constant integers. That's why Benoit's test<0>()
would compile.
For a full specialization (there is no partial specialization for function templates, there's just overloading; class templates, however, do have partial specialization), you have to always provide an empty template parameter list (template<>
) and put the types to specialize for behind the identifier test<int>
.