Question

I'm struggling with templates ! Consider the following code:

template <typename... Ts> struct Sequence {};

template<unsigned N> struct B {
  template<unsigned P> struct C {
    typedef int type;
  };
};

Then this is perfectly correct:

template<unsigned... Is>
struct Loop2 {
  typedef Sequence< typename B<5>::C<Is>::type... > type;
};

Loop2<3,1> l;

Therefore I can't understand why this templated version:

template<unsigned N, unsigned... Is>
struct Loop3 {
  typedef Sequence< typename B<N>::C<Is>::type... > type;
};

isn't accepted by the compiler. It raise the following error:

essai.cpp:29:51: error: template argument 1 is invalid
   typedef Sequence< typename B<N>::C<Is>::type... > type;

For the information I got this with

g++ (SUSE Linux) 4.8.1 20130909 [gcc-4_8-branch revision 202388]

Thanks for any help !

By the way: any suggestion for a better title is welcome !

Was it helpful?

Solution

As B is not a specific type anymore, you need to flag C with the template keyword as it is depenend on the value of N. Using the follwing code should work:

template<unsigned N, unsigned... Is>
struct Loop3 {
  typedef Sequence< typename B<N>::template C<Is>::type... > type;
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top