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) ... };}
1
  • 3
    How would we know? There are no error messages. Commented Aug 1, 2013 at 17:08

2 Answers 2

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) ... };}
                                       ^^^^^^^^^
Sign up to request clarification or add additional context in comments.

Comments

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>().

1 Comment

If replace std::get<Idx-1>(elements)._N with constatnt, say, 3, will be the same error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.