[ create a new paste ] login | about

Link: http://codepad.org/0unH4XxA    [ raw code | output | fork ]

lanzkron - C++, pasted on Jul 27:
    class nil {};

    template <class Head, class Tail = nil>
    struct type_list {
        typedef Head head;
        typedef Tail tail;
    };

    template <class List>
    struct foo;

    template <class Head, class Tail>
    struct foo<type_list<Head, Tail> >{
        template <class T>
        static void* bar(T* obj, size_t size)
        {
            if (sizeof(Head) == size) 
                return reinterpret_cast<Head*>(obj);
        
            // Otherwise check the rest of the list
            return foo<Tail>::bar(obj, size);
        }
    };

 
    template <>
    struct foo<nil>
    {
        template <class T>
        static void* bar(T*, size_t)
        {
            return NULL;
        }
    };

#include <iostream>

    int main()
    {
        int n = 3;
        void *p = foo<type_list<char, 
                         type_list<bool, 
                          type_list<double, 
                           type_list<long> > > >
                        >::bar(&n, 4); 
        std::cout<< p << std::endl;
    }


Output:
1
0xbfa70d1c


Create a new paste based on this one


Comments: