如何使用作为模板参数的模板
How to work with template which are template parameter
我正在尝试制作某种类型的双重列表。在这种情况下,它是 "BoardCell" 的双重列表。
这是我对列表的实现:
template<typename T, typename... TT>
struct List {
typedef T head;
typedef List<TT...> next;
constexpr static int size = 1 + next::size;
};
template<typename T>
struct List<T> {
typedef T head;
constexpr static int size = 1;
};
这里是 BoardCell 的实现:(当 "CellType" 和 "Direction" 是枚举时)
template<CellType CT, Direction D, int L>
struct BoardCell {
constexpr static CellType type = CT;
constexpr static Direction direction = D;
constexpr static int length = L;
};
现在我正在尝试制作 GameBoard。
这是我的尝试,但我找不到它不起作用的原因:
template<template<template<template<CellType C, Direction D, int L> class BoardCell> class List> class List>
struct GameBoard {
typedef List<List<BoardCell<C, D, L>>> board;
};
(是的,这是 x3 嵌套模板 :( )
我相信模板的行是好的,板子的typedef是这里的问题。
编辑 - 添加:
这是 GameBoard 输入的示例:
typedef GameBoard<List< List< BoardCell<EMPTY, UP, 0>, BoardCell<EMPTY, UP, 0>>,
List< BoardCell<X, RIGHT, 1>, BoardCell<A, UP, 1>>,
List< BoardCell<EMPTY, UP, 0>, BoardCell<EMPTY, UP, 0>>>> gameBoard;
我不使用 std::tuple 因为这是家庭作业的一部分,我们也需要实施该列表。
BoardCell
in GameBoard
与 BoardCell
模板完全无关,同上 two List
s.
您没有定义任何种类的东西
template<template<template<CellType, Direction, int> class> class> class
你可以用它来传递你对 GameBoard
的定义。最有可能的是,您应该传递 List
模板的 特定实例化 ,例如
List< List< BoardCell<EMPTY, UP, 0>, BoardCell<EMPTY, UP, 0>>,
List< BoardCell<X, RIGHT, 1>, BoardCell<A, UP, 1>>,
List< BoardCell<EMPTY, UP, 0>, BoardCell<EMPTY, UP, 0>>>
但是你有一个 do-nothing 定义
template<typename Board>
struct GameBoard {
typedef Board board;
};
我正在尝试制作某种类型的双重列表。在这种情况下,它是 "BoardCell" 的双重列表。 这是我对列表的实现:
template<typename T, typename... TT>
struct List {
typedef T head;
typedef List<TT...> next;
constexpr static int size = 1 + next::size;
};
template<typename T>
struct List<T> {
typedef T head;
constexpr static int size = 1;
};
这里是 BoardCell 的实现:(当 "CellType" 和 "Direction" 是枚举时)
template<CellType CT, Direction D, int L>
struct BoardCell {
constexpr static CellType type = CT;
constexpr static Direction direction = D;
constexpr static int length = L;
};
现在我正在尝试制作 GameBoard。 这是我的尝试,但我找不到它不起作用的原因:
template<template<template<template<CellType C, Direction D, int L> class BoardCell> class List> class List>
struct GameBoard {
typedef List<List<BoardCell<C, D, L>>> board;
};
(是的,这是 x3 嵌套模板 :( ) 我相信模板的行是好的,板子的typedef是这里的问题。
编辑 - 添加: 这是 GameBoard 输入的示例:
typedef GameBoard<List< List< BoardCell<EMPTY, UP, 0>, BoardCell<EMPTY, UP, 0>>,
List< BoardCell<X, RIGHT, 1>, BoardCell<A, UP, 1>>,
List< BoardCell<EMPTY, UP, 0>, BoardCell<EMPTY, UP, 0>>>> gameBoard;
我不使用 std::tuple 因为这是家庭作业的一部分,我们也需要实施该列表。
BoardCell
in GameBoard
与 BoardCell
模板完全无关,同上 two List
s.
您没有定义任何种类的东西
template<template<template<CellType, Direction, int> class> class> class
你可以用它来传递你对 GameBoard
的定义。最有可能的是,您应该传递 List
模板的 特定实例化 ,例如
List< List< BoardCell<EMPTY, UP, 0>, BoardCell<EMPTY, UP, 0>>,
List< BoardCell<X, RIGHT, 1>, BoardCell<A, UP, 1>>,
List< BoardCell<EMPTY, UP, 0>, BoardCell<EMPTY, UP, 0>>>
但是你有一个 do-nothing 定义
template<typename Board>
struct GameBoard {
typedef Board board;
};