-1

Suppose abc is defined as:

template <typename T>
struct abc;

and xyz as

template <typename ...Ts>
struct xyz;

Is it possible to declare an attribute in xyz such as std::tuple<abc<T>> for each T in Ts?

cigien
  • 57,834
  • 11
  • 73
  • 112
canellas
  • 491
  • 5
  • 17
  • If you want to pluck out just one of the `Ts` and then turn it into a `std::tuple>`, you can use `std::tuple_element`: `template using xyz = std::tuple>>>;` It's not entirely clear what you're looking for. Can you give an example usage of the thing you want? – Raymond Chen Sep 02 '22 at 20:50
  • @RaymondChen, below is the answer and an example to illustrate – canellas Sep 02 '22 at 21:45

1 Answers1

1

It is possible using std::tuple<abc<Ts>...>, as the example below ilustrates

#include <iostream>
#include <tuple>

using namespace std;

template <typename t> struct abc {
  abc() = default;
  abc(int p) : i(p) {}
  friend std::ostream &operator<<(std::ostream &out, const abc &x) {
    out << x.i;
    return out;
  }
  int i;
};

template <typename... ts> struct xyz {
  template <typename t> void add(int i) {
    std::get<abc<t>>(m_tuple) = abc<t>(i);
  }

  template <typename t> void print() {
    cout << std::get<abc<t>>(m_tuple) << endl;
  }

  using tuples = std::tuple<ts...>;
  typedef std::tuple<abc<ts>...> tuple;
  tuple m_tuple;
};

struct a {};

struct b {};

struct c {};

int main() {
  using v = xyz<a, b, c>;

  v v0;

  v0.add<a>(-9);
  v0.add<b>(22);
  v0.add<c>(-235);

  v0.print<a>();
  v0.print<b>();
  v0.print<c>();

  return 0;
}
Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
canellas
  • 491
  • 5
  • 17