Is it possible to deduce a non-type template parameter from a template function parameter?
Consider this simple template:
template <int N> constexpr int factorial()
{
return N * factorial<N - 1>();
}
template <> constexpr int factorial<0>()
{
return 1;
}
template <> constexpr int factorial<1>()
{
return 1;
}
I would like to be able to change factorial
so that I can alternatively call it like this:
factorial(5);
and let the compiler figure out the value of N at compile time. Is this possible? Maybe with some fancy C++11 addition?
Can't be done, unless you have a time machine.
The parameter to the function is handled at runtime. Yes, in your case it's a literal constant, but that's a special case.
In function definitions, the parameter types are fixed at compile-time (and thus, can be used to deduce template parameters), but parameter values are only fixed at runtime.
Why do you need this? Is it just so you don't have to type the <>
's?