2

In addition to topics: Heterogeneous sequence generator and Variadic template heterogeneous container In the code below, I tried to handle sequence of objects in recurrent manner using templates - current object in tuple-sequence gets parameter from previous object:

namespace spec
{
    template <int... Idx>
    struct index { };

    template <int N, int... Idx>
    struct sequence : sequence<N - 1, N - 1, Idx...> { };

    template <int... Idx>
    struct sequence<1, Idx...> : index<Idx...> { };
}
template<int N> 
struct A
{
    A() : _N(N) {}
    template<int PrevN> void print_prevN(){std::cout<<PrevN<<std::endl;}
    int _N;
};

template<int N> 
struct B
{
    B(): _N(N){}
    template<int PrevN> void print_prevN(){std::cout<<PrevN<<std::endl;}
    int _N;
};

template<typename...Arg>
class HeterogenousContainer
{
public:

    void process(){process(spec::sequence<sizeof...(Arg)>());}

private:
    std::tuple<Arg...> elements;   
    template <int... Idx> void process(spec::index<Idx...>)//this function generates an error
    {auto aux = { (std::get<Idx>(elements).print_prevN<std::get<Idx-1>(elements)._N>(), 0) ... };}
};
int main()
{  
   HeterogenousContainer<A<3>, B<4>, B<2>> obj;
}

What's wrong?

error: expected primary-expression before «)» token

This error in the line:

{auto aux = { (std::get<Idx>(elements).print_prevN<std::get<Idx-1>(elements)._N>(), 0) ... };}
Community
  • 1
  • 1
gorill
  • 1,623
  • 3
  • 20
  • 29

2 Answers2

5

The compiler doesn't know that print_prevN is a function template (it's a dependent name), so the succeeding < and > tokens are parsed as comparison operators. Write:

{auto aux = { (std::get<Idx>(elements).template print_prevN<std::get<Idx-1>(elements)._N>(), 0) ... };}
                                       ^^^^^^^^^
Community
  • 1
  • 1
ecatmur
  • 152,476
  • 27
  • 293
  • 366
0

std::get<Idx-1>(elements)._N is not a constant expression, and thus not suitable as a template parameter in the expression std::get<Idx>(elements).print_prevN<std::get<Idx-1>(elements)._N>().

Casey
  • 41,449
  • 7
  • 95
  • 125