Boost logo

Boost :

Subject: Re: [boost] Reimplementation of the MPL for C++11
From: pfultz2 (pfultz2_at_[hidden])
Date: 2013-10-19 11:45:41


> But knowing the forward-only nature of
parameter packs, I wonder how you can implement a random-access sequence
like vector without (a) instantiating O(N) templates, like tuple, or (b)
using the preprocessor and living with arbitrary limits.

The O(N) instantiation would happen at creation of the vector, not at
lookup.
Something like this:

    template<long N, class... Ts>
    struct vector_base;

    template<long N, class T, class... Ts>
    struct vector_base<N, T, Ts...>
    : vector_base<N+1, Ts...>
    {
        using vector_base<N+1, Ts...>::item_;
        static T item_(long_<N>);
    };

    template<long N>
    struct vector_base<N>
    {
        static void item_();
    };

    template<class... Ts>
    struct vector
    : vector_base<0, Ts...>
    {
    };

Then we have O(1) lookup using decltype:

    template<class Vector, class Index>
    struct at
    {
        typedef decltype(Vector::item_(Index())) type;
    };

I haven't tested this code yet, but the Boost.MPL works in a similiar way
for
compilers that suppor typeof. Of course generating the `item_` overloads
with
preprocessor perhaps could be faster than expaning the parameter pack using
base classes, since it requires O(N) instantiations. However, if the
prerpocessor version is faster, the `vector` could be specialized up to an
arbitary limit to be generated by the preprocessor, and then after that it
would use the base classes to generate the overloads.

Paul

--
View this message in context: http://boost.2283326.n4.nabble.com/Reimplementation-of-the-MPL-for-C-11-tp4653313p4653366.html
Sent from the Boost - Dev mailing list archive at Nabble.com.

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk