November 23rd, 2004

полосатая свинья

FP in C++

template<class T, class U>
struct TList
{
    typedef T Head;
    typedef U Tail;
};

class NullType {};

typedef TList<char, TList<short, TList<int,
    TList<long, NullType> > > > SignedIntTypes;

template<class TList> struct Length;

template<>
struct Length<NullType>
{
    enum { value = 0 };
};

template<class T, class U>
struct Length<TList<T, U> >
{
    enum { value = 1 + Length<U>::value };
};

template<class TList, class T> struct IndexOf;

template<class T>
struct IndexOf<NullType, T>
{
    enum { value = -1 };
};

template<class U, class T>
struct IndexOf<TList<T, U>, T>
{
    enum { value = 0 };
};

template<class U, class V, class T>
struct IndexOf<TList<U, V>, T>
{
private:
    enum { temp = IndexOf<V, T>::value };
public:
    enum { value = temp!=-1 ? temp+1 : -1 };
};