获取模板化模板类型

Get templated, template type

本文关键字:类型 获取      更新时间:2023-10-16

我正在创建一个小型的'通用'寻路类,该类采用类类型的Board,它将在其上查找路径,

//T - Board class type
template<class T>
class PathFinder
{...}

Board也被模板化以保存节点类型。(这样我就可以在 2D 或 3D 向量空间上找到路径)。

我希望能够为PathFinder声明和定义一个成员函数,该函数将采用这样的参数

//T - Board class type
PathFinder<T>::getPath( nodeType from, nodeType to);

如何对作为参数馈送到函数中的TnodeType的节点类型执行类型兼容性?

如果我

明白你想要什么,给board一个类型成员并使用它:

template<class nodeType>
class board {
  public:
    typedef nodeType node_type;
  // ...
};
PathFinder<T>::getPath(typename T::node_type from, typename T::node_type to);

如果您无法更改board,您也可以进行模式匹配:

template<class Board>
struct get_node_type;
template<class T>
struct get_node_type<board<T> > {
  typedef T type;
};
PathFinder<T>::getPath(typename get_node_type<T>::type from, typename get_node_type<T>::type to);

您可以在类定义中typedef nodeType

typedef typename T::nodeType TNode;
PathFinder<T>::getPath( TNode from, TNode to);