Functions can not be specialized partially. Instead, you can use classes, which can be partially specialized:
#include <cstdio>
template<typename T, unsigned n>
struct A
{
T values[n];
};
template<typename T>
struct component_counter
{
static unsigned count()
{
return 1;
}
};
template<typename T, unsigned n>
struct component_counter<A<T, n> >
{
static unsigned count()
{
return n;
}
};
int main()
{
printf("%d\n", component_counter<A<float, 4> >::count());//should be 4
printf("%d\n", component_counter<float>::count());//should be 1
return 0;
}
And in this case the count() does not actually have to be a function at all!
You can do it like this:
#include <cstdio>
template<typename T, unsigned n>
struct A
{
T values[n];
};
template<typename T>
struct component_counter
{
static const unsigned count=1;
};
template<typename T, unsigned n>
struct component_counter<A<T, n> >
{
static const unsigned count=n;
};
int main()
{
printf("%d\n", component_counter<A<float, 4> >::count);//should be 4
printf("%d\n", component_counter<float>::count);//should be 1
return 0;
}
Which is less code. The caveat is that count has to be an integral type for this to work.