gpt4 book ai didi

c++ - 如何使用作为模板参数的模板

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:02:44 24 4
gpt4 key购买 nike

我正在尝试制作某种类型的双列表。在这种情况下,它是“BoardCell”的双重列表。这是我对 List 的实现:

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,因为这是家庭作业的一部分,我们还需要实现列表。

最佳答案

GameBoard 中的

BoardCellBoardCell 模板完全无关,两个 Lists.

没有你定义的任何东西

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​>>>

但是你有一个什么都不做的定义

template<typename Board>
struct GameBoard {
typedef Board board;
};

关于c++ - 如何使用作为模板参数的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56598046/

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