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
?
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
?
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;
}