因此,我正在阅读Alexandrescu关于如何创建类型列表的这篇文章,但是本文中的以下代码没有用MSVC/GCC编译:
template <class H, class T>
struct typelist
{
typedef H head;
typedef T tail;
};
class null_typelist {};
template <class T1>
struct cons<T1, null_typelist, null_typelist,
null_typelist>
{
typedef typelist<T1, null_typelist> type;
};
template <class T1, class T2>
struct cons<T1, T2, null_typelist, null_typelist>
{
typedef typelist<T1, typelist<T2,
null_typelist> > type;
};
template <class T1, class T2, class T3>
struct cons<T1, T2, T3, null_typelist>
{
typedef typelist<T1, typelist<T2, typelist<T3,
null_typelist> > > type;
};
template <class T1, class T2, class T3, class T4>
struct cons
{
typedef typelist<T1, typelist<T2, typelist<T3,
typelist<T4, null_typelist> > > > type;
};
typedef cons<float, double, long double>::type
floating_point_types;我得到以下错误:
a.cpp:21:8: error: 'cons' is not a class template
struct cons<T1, null_typelist, null_typelist,
^~~~
a.cpp:28:49: error: wrong number of template arguments (4, should be 1)
struct cons<T1, T2, null_typelist, null_typelist>
^
a.cpp:22:18: note: provided for 'template<class T1> struct cons'
null_typelist>
^
a.cpp:35:38: error: wrong number of template arguments (4, should be 1)
struct cons<T1, T2, T3, null_typelist>
^
a.cpp:22:18: note: provided for 'template<class T1> struct cons'
null_typelist>
^
a.cpp:42:8: error: redeclared with 4 template parameters
struct cons
^~~~
a.cpp:22:18: note: previous declaration 'template<class T1> struct cons' used 1
template parameter
null_typelist>
^
a.cpp:48:40: error: wrong number of template arguments (3, should be 1)
typedef cons<float, double, long double>::type
^
a.cpp:22:18: note: provided for 'template<class T1> struct cons'
null_typelist>
^
a.cpp:49:5: error: expected initializer before 'floating_point_types'
floating_point_types;
^~~~~~~~~~~~~~~~~~~~我在C++98中需要这个库作为我正在编写的库,任何类似于各种模板或第三方(如boost::mpl )的东西都不是一种选择。
那么问题是什么呢?我不太喜欢模板元编程(目前).
发布于 2017-04-01 09:45:35
我不知道是否可以在msvc中工作(但可以使用我的g++),但是,在您的代码中,您可以声明cons的部分专门化。
因此,在第一个部分专门化之前(关闭主题:我建议对模板参数使用typename而不是class )
template <typename T1>
struct cons<T1, null_typelist, null_typelist,
null_typelist>
{
typedef typelist<T1, null_typelist> type;
};您应该插入一个泛型声明,我认为这个声明可能有点像
template <typename,
typename = null_typelist,
typename = null_typelist,
typename = null_typelist>
struct cons;https://stackoverflow.com/questions/43154805
复制相似问题