You need to pass an integral value in a template, Both, your first and second template, will not work if the type T is not an integral type.
You can pass Traits as a typed template parameter to specify the value N:
#include <iostream>
// error: ‘double’ is not a valid type for a template non-type parameter
template <typename T, T N> struct X0;
// error: ‘double’ is not a valid type for a template non-type parameter
template <typename T, T N, int = 0> struct X1;
template <typename T, T N>
struct IntegralTraits {
static constexpr T Value() { return N; }
};
template <typename T, typename Traits = void>
struct X2 {
static constexpr T Value() { return Traits::Value(); }
};
template <typename T>
struct X2<T, void> {
static constexpr T Value() { return T(); }
};
int main() {
// error: ‘double’ is not a valid type for a template non-type parameter
// X0<double, 0>();
// error: ‘double’ is not a valid type for a template non-type parameter
// X1<double, 0>();
X2<int> a;
X2<double, IntegralTraits<int, 1>> b;
std::cout.precision(2);
std::cout << std::fixed << a.Value() << ", "<< b.Value() << '\n';
return 0;
}
If you limit yourself to integral types pick a large one:
template <typename T, std::size_t N = 0> struct X {};