I\'m testing out user defined literals. I want to make _fac return the factorial of the number.
Having it call a constexpr function works,
This is how I ended up doing it:
template
constexpr t pow(t base, int exp) {
return (exp > 0) ? base * pow(base, exp-1) : 1;
};
template struct literal;
template <> struct literal<> {
static const unsigned int to_int = 0;
};
template struct literal {
static const unsigned int to_int = (c - '0') * pow(10, sizeof...(cv)) + literal::to_int;
};
template struct factorial {
static const unsigned int value = N * factorial::value;
};
template <> struct factorial<0> {
static const unsigned int value = 1;
};
template
constexpr unsigned int operator "" _fac()
{
return factorial::to_int>::value;
}
Huge thanks to KerrekSB!