[英]how do I fix this c++ typelist template compile error?
(from reading chapter 3 of modern c++ design) (摘自现代c ++设计的第3章)
typelist.hpp: typelist.hpp:
class NullType {};
struct EmptyType {};
template <class T, class U>
struct Typelist
{
typedef T Head;
typedef U Tail;
};
#define TYPELIST_1(T1) Typelist<T1, NullType>
#define TYPELIST_2(T1, T2) Typelist<T1, TYPELIST_1(T2) >
#define TYPELIST_3(T1, T2, T3) Typelist<T1, TYPELIST_2(T2, T3) >
#define TYPELIST_4(T1, T2, T3, T4) Typelist<T1, TYPELIST_3(T2, T3, T4) >
#define TYPELIST_5(T1, T2, T3, T4, T5) Typelist<T1, TYPELIST_4(T2, T3, T4, T5) >
#define TYPELIST_6(T1, T2, T3, T4, T5, T6) Typelist<T1, TYPELIST_5(T2, T3, T4, T5, T6) >
namespace TL
{
template <class TList> struct Length;
template <> struct Length<NullType>
{
enum { value = 0 };
};
template <class T, class U>
struct Length< Typelist<T, U> >
{
enum { value = 1 + Length<U>::value };
};
template <class Head, class Tail>
struct TypeAt<Typelist<Head, Tail>, 0>
{
typedef Head Result;
};
template <class Head, class Tail, unsigned int i>
struct TypeAt<Typelist<Head, Tail>, i>
{
typedef typename TypeAt<Tail, i-1>::Result Result;
};
}
main.cpp main.cpp中
#include "typelist.hpp"
Typelist<int, double> foo;
int main() {
}
g++ main.cpp g ++ main.cpp
typelist.hpp:37: error: ‘TypeAt’ is not a template
typelist.hpp:43: error: type/value mismatch at argument 2 in template parameter list for ‘template<class Head, class Tail> struct TL::TypeAt’
typelist.hpp:43: error: expected a type, got ‘i’
Why do I get this error? 为什么会出现此错误? How do I fix this? 我该如何解决?
Looks like you're missing a forward declaration. 好像您缺少前向声明。
This is a partial specialization: 这是部分专业化:
template <class Head, class Tail>
struct TypeAt<Typelist<Head, Tail>, 0>
But the compiler has no idea what it's a specialization of. 但是编译器不知道它是什么专业化的。 Add this before it: 在此之前添加:
template <class List, unsigned Index>
struct TypeAt;
This let's the compiler know: "There is a class TypeAt
which has two template parameters." 这让编译器知道:“有一个TypeAt
类,它具有两个模板参数。” So now when you specialize it, the compiler knows what class you're talking about. 因此,现在当您对其进行专门化时,编译器便知道了您在说什么类。
Note, your usage of Typelist
is incorrect. 请注意,您对Typelist
的使用不正确。 These algorithm's are sentinel-terminated . 这些算法以哨兵终止 。 This means, like C-strings, they expect the data to be concluded with a special value. 这意味着,像C字符串一样,他们希望数据以特殊值结束。 In our case, this is NullType
. 在我们的例子中,这是NullType
。
So, take Éric 's advice. 因此,请听Éric的建议。 (ie hint: if you found his answer helpful, up-vote it.) (即提示:如果您认为他的回答有用,请对其进行投票。)
Typelist is designed to be used recursively: It expects its second templater parameter to be either another Typelist, or NullType (signaling the end of recursion). Typelist被设计为可递归使用:它期望其第二个templater参数是另一个Typelist或NullType(表示递归结束)。
To define foo, you should write: 要定义foo,您应该编写:
TYPELIST_2(int, double) foo;
or, if you want to avoid macros: 或者,如果您想避免使用宏:
Typelist<int, Typelist<double, NullType> > foo;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.