From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2002-10-30 20:47:05


David Abrahams wrote:
> I'm afraid it's not a reference. Looking back, I don't see how it
> could be.
>
> Oh well. He has a very interesting technique of using multidimensional
> array types to represent sequences of integers:
>
> char[i0][i1][i2][i3]...
>
> I think many compilers might like this better than list<int_c<i0>,
> int_c<i1>, ...>. We'd just need to define the appropriate iterators.

Yep:

    namespace boost { namespace mpl {

    struct array_list_c {};

    template< typename T > struct list_c_iterator;
    template< typename T, long N > struct list_c_iterator<T[N]>
    {
        typedef int_c<N> type;
        typedef list_c_iterator<T> next;
    };

    template<> struct list_c_iterator<array_list_c>
    {
    };

    template< typename T > struct begin<T[]>
    {
        typedef list_c_iterator<T> type;
    };

    template< typename T > struct end<T[]>
    {
        typedef list_c_iterator<array_list_c> type;
    };

    template< typename T > struct sequence_tag<T[]>
    {
        typedef void type;
    };

    }}

    using namespace boost::mpl;

    typedef array_list_c list_0[]; // empty list
    typedef array_list_c list_1[][10]; // one-element list
    typedef array_list_c list_2[][5][7][1][2];

    BOOST_STATIC_ASSERT(front<list_1>::type::value == 10);
    BOOST_STATIC_ASSERT(front<list_2>::type::value == 5);
    
    typedef fold<list_2, int_c<0>, plus<> >::type sum;
    BOOST_STATIC_ASSERT(sum::value == 15);

Isn't that cool or what? :)

The only catch is that these "lists" cannot contain negative numbers, at
least directly (without some wrapper casting to unsigned values and back
behind the scene).

Aleksey