I think you need something like this:
template<typename T>
struct triplet
{
T first, middle, last;
};
template<typename T>
triplet<T> make_triplet(const T &m1, const T &m2, const T &m3)
{
triplet<T> ans;
ans.first = m1;
ans.middle = m2;
ans.last = m3;
return ans;
}
Examples of usage:
triplet<double> aaa;
aaa = make_triplet<double>(1.,2.,3.);
cout << aaa.first << " " << aaa.middle << " " << aaa.last << endl;
triplet<bool> bbb = make_triplet<bool>(false,true,false);
cout << bbb.first << " " << bbb.middle << " " << bbb.last << endl;
I'm using this and it is enough for my purposes... If you want different types, though, just do some modifications:
template<typename T1, typename T2, typename T3>
struct triplet
{
T1 first;
T2 middle;
T3 last;
};
template<typename T1, typename T2, typename T3>
triplet<T1,T2,T3> make_triplet(const T1 &m1, const T2 &m2, const T3 &m3)
{
triplet<T1,T2,T3> ans;
ans.first = m1;
ans.middle = m2;
ans.last = m3;
return ans;
}
And the usage will be very similar:
triplet<bool,string,myDouble> ccc;
ccc = make_triplet<bool,string,double>(false,"AB",3.1415);
ccc.middle = "PI";
cout << ccc.first << " " << ccc.middle << " " << ccc.last << endl;
std::tuple
. – Schematizestd::tuple
(non-C++11 compliant compiler, for instance), considerstd::pair<T, std::pair<U, V> >
. – Onionskinstd::pair
. How often do you really want elements calledfirst
andsecond
(rather than e.g.key
andvalue
, orname
andsurname
). – Ciprianmytriple.second.second
is much more confusing thanmytriple.third
. – Jew