Generic Name
Generic Name

Reputation: 1270

How do I get index of elements in parameter pack

How do I get the following to put the index of the parameter pack element in the tuple?

template< typename... Ts >
class ClassA {
public:
    ClassA( Ts... ts ) : tup( make_tuple( ts, 0 )... ) {}
    // I would like it to expand to this.
    // ClassA( T0 ts0, T1 ts1 ) : tup( make_tuple( ts0, 0 ), make_tuple(ts1, 1) ) {}
    tuple<tuple<Ts, size_t>...> tup;
};

void main() {
    vector<int> a ={ 2, 4, 5 };
    list<double> b ={ 1.1, 2.2, 3.3 };
    ClassA<vector<int>, list<double>, vector<int>, list<double>> mm( a, b, a, b );
}

Thanks.

Upvotes: 3

Views: 5159

Answers (2)

Nir Friedman
Nir Friedman

Reputation: 17694

You can avoid making the constructor a template, and index sequence in general, by simply incrementing an integer as part of the pack expansion:

template <typename ... Ts>
class ClassA
 {
   private:
      ClassA (size_t i, Ts ... ts)
         : tup { std::make_tuple(ts, i++) ... }
       { }

   public:
      ClassA (Ts ... ts)
         : ClassA(0, ts...)
       { }

      std::tuple<std::tuple<Ts, size_t>...> tup;
 };

I should note that gcc is currently giving me a warning about lacking a sequence point, but it seems like a false positive (and in any case this can be easily fixed by writing a tiny function and calling it).

Upvotes: 3

max66
max66

Reputation: 66210

It seems to me (if you can use at least C++14) a work for a delegating constructor

template <typename ... Ts>
class ClassA
 {
   private:
      template <std::size_t ... Is>
      ClassA (std::index_sequence<Is...> const &, Ts ... ts)
         : tup { std::make_tuple(ts, Is) ... }
       { }

   public:
      ClassA (Ts ... ts)
         : ClassA(std::make_index_sequence<sizeof...(Ts)>{}, ts...)
       { }

      std::tuple<std::tuple<Ts, std::size_t>...> tup;
 };

In C++11 this doesn't works because std::index_sequence and std::make_index_sequence are available only starting from C++14 but it isn't difficult to find (or develop) C++11 substitutes.

Upvotes: 8

Related Questions