型リストからindex_sequenceを作る

組込のfolding-exprを再帰深度O(1)とみなすと全体でも再帰深度O(1)

melpon.org

#include <type_traits>
#include <utility>

template<typename... Types>
struct make_indices_from_types {
  private:
    template <std::size_t N, std::size_t...>
    struct make_indices {
        template <std::size_t M, std::size_t... Indices>
        make_indices<N + M, M, Indices...> operator +(const make_indices<M, Indices...>&);
    };
    template <typename> static constexpr int step = 1;
    template <std::size_t... Indices>
    static std::index_sequence<sizeof...(Types) - Indices...> impl(const make_indices<Indices...>&);

  public:
    using type = decltype(impl((make_indices<step<Types>>{} + ...)));
};

int main() {
    static_assert(std::is_same<
                    typename make_indices_from_types<int, char, double, float, void*, bool>::type,
                    std::index_sequence<0, 1, 2, 3, 4, 5>
                  >::value);
}