I've made a struct to retrieve types from a list, so-to-speak. This version compiles.
template<uint16_t index, class first, class...rest>
struct type_at {
type_at() = delete;
static constexpr inline auto create() {
if constexpr(index == 0) {
struct conditional {
constexpr inline conditional() = default;
using type = first;
}; return conditional();}
else {
struct conditional {
constexpr inline conditional() = default;
using type = typename type_at<index - 1, rest...>::type;
}; return conditional();}
}
using type = typename decltype(create())::type;
};
Messing around with the implementation, I made a new version that moves the index template parameter to 'type' and 'create' like so.
template<class first, class...rest>
struct type_at {
type_at() = delete;
template<uint16_t index>
static constexpr inline auto create() {
if constexpr(index == 0) {
struct conditional {
constexpr inline conditional() = default;
using type = first;
}; return conditional();}
else {
struct conditional {
constexpr inline conditional() = default;
using type = typename type_at<rest...>::type<index - 1>;
}; return conditional();}
}
template<uint16_t index>
using type = typename decltype(create<index>())::type;
};
This version fails with the compilation error expected ; before < token
in create() where I access the type of a struct (...type<index - 1>;
).
My question is: why? It's not obvious to me why the compiler wouldn't expect template arguments. I tried type.template, but that didn't help. I'm guessing I'm missing something, so any help understanding would be appreciated.