Сообщение Нерекурсивный tuple от 19.05.2016 6:44
Изменено 20.05.2016 3:58 Шахтер
Ловите
Вообщем, новый С++, конечно, вставляет.
/**/
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 ;Вообщем, новый С++, конечно, вставляет.
Tuple<short,int,long> tuple;
tuple.ref<1>()=1;
tuple.ref<2>()=2;
tuple.ref<3>()=3;Нерекурсивный tuple
Ловите
Усовершенствованная версия здесь.
Вообщем, новый С++, конечно, вставляет.
Усовершенствованная версия здесь.
/**/
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 ;Вообщем, новый С++, конечно, вставляет.
Tuple<short,int,long> tuple;
tuple.ref<1>()=1;
tuple.ref<2>()=2;
tuple.ref<3>()=3;