Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Non-recursive tuple implementation

616 views
Skip to first unread message

Sergey Strukov

unread,
May 19, 2016, 3:50:13 PM5/19/16
to

Here is an example of non-recursive tuple implementation using the modern C++ features. Enjoy!

/**/

template <class T>
struct IncList
{
template <class S>
IncList<T> operator - (IncList<S>) { return {}; }

using Type = T ;
};

/**/

template <int Ind>
struct PickTypeInd
{
template <class T>
PickTypeInd<Ind-1> operator - (IncList<T>) { return {}; }
};

template <>
struct PickTypeInd<1>
{
template <class T>
IncList<T> operator - (IncList<T>) { return {}; }
};

/**/

template <int Ind,class ... TT>
using PickType = typename decltype( ( PickTypeInd<Ind>() - ... - IncList<TT>() ) )::Type ;

/**/

template <int ... IList>
struct IntList
{
template <class T>
IntList<IList...,1+sizeof ... (IList)> operator + (IncList<T>) { return {}; }
};

/**/

template <class ... TT>
struct TupleFactory
{
template <int Ind>
using Type = PickType<Ind,TT...> ;

template <int Ind>
struct Field
{
Type<Ind> field;
};

template <int ... IList>
struct Tuple : Field<IList>...
{
template <int I>
auto & ref() { return ((Field<I> *)this)->field; }
};

template <int ... IList>
static constexpr Tuple<IList...> FromList(IntList<IList...>) { return {}; }

using Ret = decltype( FromList( ( IntList<>() + ... + IncList<TT>() ) ) ) ;
};

template <class ... TT>
using Tuple = typename TupleFactory<TT...>::Ret ;



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

0 new messages