I was testing this code (https://godbolt.org/z/fe6hhbeqW)...
// Returns the nth type in a parameter pack of types (ommited for clarity)
// template <std::size_t N, typename...Ts>
// nth_type{}
template <typename... Ts>
struct Typelist{
template <typename T>
consteval static std::size_t pos() noexcept {
for(std::size_t i{}; i < sizeof...(Ts); ++i) {
using TN = nth_type_t<i, Ts...>;
if (std::is_same_v<T, TN>)
return i;
}
return sizeof...(Ts);
}
};
and I was puzzled for it not working. GCC and clang agree on i
not being a constant expression, so they refuse to let me pass it as a template parameter. However, i
is clearly known at compile-time so, to my limited understanding, the compiler shouldn't have any problem to use it to instantiate the template.
Is there a reason for this not to work? Will it work in the future? I have tested with trunk versions of both compilers, with same result.
i
should be used when? – Orlinai
is a constant, its value cannot be changed right?? butfor loop
changes the value ofi
on every loop? – Pugging