0

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.

Anthony
  • 1,015
  • 8
  • 22
  • 3
    Did you try with :: instead of .? typename type_at:: `template` type; – JVApen Jul 22 '18 at 18:24
  • @JVApen that worked! I didn't realize template was possible after the `::` operator, but it makes sense now that I think about it. Thank you! – Anthony Jul 22 '18 at 18:26

1 Answers1

1

You do indeed need . template

However, as you don't have an instance, this has to be ::, like with the static methods.

 using type = typename type_at<rest...>::template type<index - 1>;
JVApen
  • 11,008
  • 5
  • 31
  • 67