Question

I need to implement some variadic template container class with heterogeneous elements, which allows to iterate by these elements. My first idea is make class with std::tuple member with variadic arguments, but getting elements from tuple by array-like manner (via loops) is impossible:

struct A {void prnt(){std::cout<<"A\n";} };    
struct B {void prnt(){std::cout<<"B\n";} };    
struct C {void prnt(){std::cout<<"C\n";} };

template<typename...Arg>
struct Prc
{
    Prc() : NumElems(sizeof...(Arg)), mems(std::make_tuple(Arg()...)){}

    int NumElems;
    std::tuple<Arg...> mems;

    void process()
    {
        for(int i=0; i<NumElems; ++i)
         std::get<i>(mems).prnt();//It's forbidden: "i" must be a constant
    }
};

int main()
{
    Prc<A,B,C> obj;
    obj.process();
}

Any ideas?

P.S. I don't want use boost heterogenous containers, like boost::variant or boost::any

Was it helpful?

Solution

Here's it done using indicies:

namespace detail
{
    template <int... Is>
    struct index { };

    template <int N, int... Is>
    struct gen_seq : gen_seq<N - 1, N - 1, Is...> { };

    template <int... Is>
    struct gen_seq<0, Is...> : index<Is...> { };
}

template <typename...Args>
struct Prc
{
    std::tuple<Args...> mems;

    template <int... Is>
    void process(detail::index<Is...>)
    {
         auto l = { (std::get<Is>(mems).prnt(), 0) ... };
    }

    void process()
    {
        process(detail::gen_seq<sizeof...(Args)>());
    }
};

OTHER TIPS

Here is some code that iterates a tuple:

struct A { void print () { clog << "A" << endl; } };
struct B { void print () { clog << "B" << endl; } };
struct C { void print () { clog << "C" << endl; } };

template<unsigned N>
struct iter
{
    template<typename T>
    static void f (T &t)
    {
        iter<N-1>::f (t);
        get<N> (t).print ();
    }
};

template<>
struct iter<0>
{
    template<typename T>
    static void f (T &t)
    {
        get<0> (t).print ();
    }
};

And the calling code:

    tuple <A,B,C> t;
    iter<tuple_size<decltype(t)>::value-1>::f (t);

I think you could modify this to fit your needs. NumElements in your code is known at compile time, so I think you would remove that member altogether.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top