C ++将模板参数包从一个可变参数模板传递到另一个可变参数模板导致编译器错误

时间:2015-08-27 21:26:57

标签: c++ templates variadic

以下代码导致编译器错误,但仅限于gcc(最多5.2)和clang(最多3.7)。 VS2015编译没有问题。 (对于g ++,你必须使用选项-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;
}

编译器输出如下:

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;
                           ^

这是编译器错误还是代码错误?对我来说,关键在于

  

期望一个类模板,得到&#39; polynomial :: number_generator&#39;

编译器输出。编译器显然没有意识到&#39; polynomial :: number_generator&#39;实际上是一个模板。你的意思是什么?

1 个答案:

答案 0 :(得分:1)

由于number_generator依赖名称,因此您必须在其前面添加template关键字:

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

作为旁注,最好避免在任何地方传递模板模板,并尝试将number_generator重写为更像元函数类:

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

这将使您的所有代码更容易,因为类型是一等公民,任何非类型的东西(即模板模板或值)都难以处理。