Actually, you do not need to use types - I consider them rather bulky, and their semantics are different. You cannot partially specialize a function, but you can overload them and make them behave like specializations by specifying default parameter values:
#include <type_traits> template <typename Iterator> int scalar_product(Iterator a, Iterator b, std::integral_constant<int, 0> = std::integral_constant<int, 0>() ) { return 0; } template <int N, typename Iterator> int scalar_product (Iterator a, Iterator b, std::integral_constant<int, N> = std::integral_constant<int, N>() ) { return (*a) * (*b) + scalar_product(a + 1, b + 1, std::integral_constant<int, N-1>() ); } int foo() { int a[] = { 1, 2, 3, 4 }; int b[] = { 1, 1, 1, 1 }; return scalar_product<4>(a, b); // returns 10 }
source share