0

The following code causes compiler errors, but only for gcc (up to 5.2) and clang (up to 3.7). VS2015 compiles without promblems. (For g++ you have to use the option -std=c++14)

//************************************************
template <int... items>
struct Sequence {
//************************************************
public:
  enum { size = sizeof...(items) };
  operator int() const { return data[rank - 1]; }
  Sequence& operator=(const Sequence& s) { rank = s.rank; return *this; }
  int get_rank() const { return rank; }
  bool first() { rank = 1; return true; }
  bool last() { rank = size; return true; }
  bool next() { return (rank < size) ? ++rank, true : false; }
  bool prev() { return (rank > 1) ? --rank, true : false; }
  bool is_first() { return rank == 1; }
  bool is_last() { return rank == size; }
protected:
  static const int data[sizeof... (items)];
  int rank = 1;
};

template <int... items> 
const int Sequence<items...>::data[sizeof...(items)] = { items... };


//************************************************
template <unsigned N, template <unsigned> class F, int... items> 
struct generate_sequence {
//************************************************
  typedef typename generate_sequence<N - 1, F, F<N>::value, items...>::result result;
};

//************************************************
template <template <unsigned> class F, int... items> 
struct generate_sequence<0, F, items...> {
//************************************************
  typedef Sequence<F<0>::value, items...> result;
};


//************************************************
template <int... coeffs>
struct polynomial {
//************************************************
  template <int var, int a0, int... ai> struct ipoly { enum { value = a0 + var * ipoly<var, ai...>::value }; };
  template <int var, int a0> struct ipoly<var, a0> { enum { value = a0 + 0 * var }; };

  template <unsigned index>
  class number_generator { public: enum { value = ipoly<index + 1, coeffs...>::value }; };
};


//************************************************
template <unsigned N>
class NaturalNumbers : public generate_sequence<N - 1, polynomial<0,1>::number_generator>::result {};
//************************************************

//************************************************
template <unsigned N, int... coeffs>
class PolynomialNumbers : public generate_sequence<N - 1, polynomial<coeffs...>::number_generator>::result {};
//************************************************

int main() {
  NaturalNumbers<10> nn;
  PolynomialNumbers<10,0,1> pn;
}

The compiler output is as follows:

bug.cpp:59:98: error: type/value mismatch at argument 2 in template parameter list for 'template<unsigned int N, template<unsigned int <anonymous> > class F, int ...items> struct generate_sequence'
 class PolynomialNumbers : public generate_sequence<N - 1, polynomial<coeffs...>::number_generator>::result {};
                                                                                                  ^
bug.cpp:59:98: note:   expected a class template, got 'polynomial<coeffs ...>::number_generator'
bug.cpp:59:101: error: expected '{' before 'result'
 class PolynomialNumbers : public generate_sequence<N - 1, polynomial<coeffs...>::number_generator>::result {};
                                                                                                     ^
bug.cpp: In function 'int main()':
bug.cpp:64:27: error: non-template type 'PolynomialNumbers' used as a template
   PolynomialNumbers<10,0,1> pn;
                           ^

Is this a compiler bug or is the code somehow wrong? For me the key lies in the line

expected a class template, got 'polynomial::number_generator'

of the compiler output. The compiler obviously does not realize that 'polynomial::number_generator' in fact is a template. What do you mean?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
mrausch
  • 3
  • 1

1 Answers1

1

Since number_generator is a dependent name, you have to prefix it with the template keyword:

template <unsigned N, int... coeffs>
class PolynomialNumbers 
: public generate_sequence<
      N - 1, 
      polynomial<coeffs...>::template number_generator
                          // ^^^^^^^^
      >::result 
{};

As a side-note, it might be better to avoid passing template templates anywhere and try to rewrite your number_generator to be more like a metafunction class:

class number_generator {
public: 
    template <unsigned index>
    using apply = ipoly<index + 1, coeffs...>;
};

That'll make all your code easier since types are first-class citizens and anything that isn't a type (i.e. a template template or a value) is much more difficult to deal with.

Barry
  • 286,269
  • 29
  • 621
  • 977