4

Consider the following code:

template<typename...>
struct C
{ /* ... */ };

template<typename T, unsigned N>
struct B
{
    using type = /* ... */;
};

template<size_t N, typename... Ts>
struct A
{
    using type = C<typename B<Ts, N-->::type...>; // decrement N sizeof...(Ts) times
};

So for example

typename A<5, int, long, void>::type

expands to

C<typename B<int, 5>::type, typename B<long, 4>::type, typename B<void, 3>::type>

Since N is a const value, this code does not compile. Is there any other way?

user7769147
  • 1,559
  • 9
  • 15

2 Answers2

9

Subtract a std::index_sequence from N

namespace detail {
    template<size_t N, typename... Ts, size_t... Is>
    C<typename B<Ts, N - Is>::type...> A_impl(std::index_sequence<Is...>);
}

template<size_t N, typename... Ts>
struct A
{
    using type = decltype(detail::A_impl<N, Ts...>(std::index_sequence_for<Ts...>{}));
};
Caleth
  • 52,200
  • 2
  • 44
  • 75
2

You can do it with a not-actually-defined-or-invoked templated static constexpr helper function:

template<size_t N, typename... Ts>
struct A
{
    template<size_t... Is>
    static constexpr auto type_getter(std::index_sequence<Is...>) -> C<typename B<Ts, N-Is>::type...>;
    using type = decltype(type_getter(std::index_sequence_for<Ts...>{}));
};

Demo

AndyG
  • 39,700
  • 8
  • 109
  • 143