gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:06:36 25 4
gpt4 key购买 nike

<分区>

以下代码会导致编译器错误,但仅适用于 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;
^

这是编译器错误还是代码有问题?对我来说关键在于行

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

编译器输出。编译器显然没有意识到“polynomial::number_generator”实际上是一个模板。什么意思?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com