更新时间:2021-10-22 15:15:05
您不需要Boost,如果您拥有C ++ 11,这只是1-liner:
You don't need Boost, this is just 1-liner if you have C++11:
constexpr uint64_t factorial(uint64_t n) {
return n == 0 ? 1 : n * factorial(n-1);
}
即使您的arg也不是编译时间常数,它也将起作用. uint64_t将与n< 21.
And it will work even if your arg is not compile time constant too. uint64_t will work with n < 21.
如果您在编译时执行此操作并乘以浮点值-则不会有转换开销(转换也将在编译时进行).
If you are doing it in compile time and multiply with floating point value - there will be no conversion overhead (conversion will be at compile time too).