So I wrote an answer here: https://stackoverflow.com/a/56569397/2642059 which strives to compute log2
at compile time like so:
template <unsigned int x>
constexpr enable_if_t<x != 0U, int> log2 = 1 + log2<x / 2U>;
template <>
constexpr int log2<1U> = 0;
This works fine but I didn't feel like I should have had to specialize:
template <unsigned int x>
constexpr enable_if_t<x != 0U, int> log2 = x < 4U ? 1 : 1 + log2<x / 2U>;
But this gives me the error:
In substitution of
template<bool _Cond, class _Tp> using enable_if_t = typename std::enable_if::type [with bool _Cond = (0u != 0u); _Tp = int]
:
prog.cpp:7:61: recursively required fromconstexpr std::enable_if_t<true, int> log2<4u>
prog.cpp:7:61: required fromconstexpr std::enable_if_t<true, int> log2<8u>
prog.cpp:10:11: required from here /usr/include/c++/6/type_traits:2523:61: error: no type namedtype
instruct std::enable_if<false, int>
Is there a way I can prevent the compiler from unrolling the recursion too far?