evaluating constexpr inside template brackets

68 Views Asked by At

Why is the following injecting a constexpr integral into a template not working

#include <iostream>
#include <type_traits>

template<typename E>
static constexpr auto enumToInt(E e) -> typename std::underlying_type<E>::type
{
    return static_cast<typename std::underlying_type<E>::type>(e);
}


template<typename E>
static consteval auto enumToIntC(E e) 
{
    constexpr auto value = enumToInt(e);
    return std::integral_constant<typename std::underlying_type<E>::type, value>{};
}

int main()
{
    enum A{ A,B};
    constexpr auto e = A::A;
    constexpr auto i = enumToInt(e); // works
    constexpr auto c = enumToIntC(e); // does not compile...
}

Why is there a limitation of injecting a clearly constexpr value into template non-type parameter not allowed? I don't understand why there is such a limitation? Is that not compiling because consteval is not yet implemented in clang-10 and decays to constexpr...?

Because this works:

template<int N> struct A {};
constexpr int value = 3;
A<value> b;
0

There are 0 best solutions below