Deduce non-type template parameter
Posted
by
pezcode
on Stack Overflow
See other posts from Stack Overflow
or by pezcode
Published on 2012-01-12T14:57:36Z
Indexed on
2013/07/02
11:05 UTC
Read the original article
Hit count: 246
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?
© Stack Overflow or respective owner