Patryk
Patryk

Reputation: 24092

Get templated, template type

I am creating a small 'generic' pathfinding class which takes a class type of Board on which it will be finding paths,

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

Whereas Board is also templated to hold the node type. (so that i can find paths on 2D or 3D vector spaces).

I would like to be able to declare and define a member function for PathFinder that will take parameters like so

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

How can I perform the type compatibility for the node type of T and nodeType that is fed into the function as parameter ?

Upvotes: 5

Views: 1447

Answers (2)

Pubby
Pubby

Reputation: 53017

If I understand what you want, give board a type member and use that:

template<class nodeType>
class board {
  public:
    typedef nodeType node_type;
  // ...
};

PathFinder<T>::getPath(typename T::node_type from, typename T::node_type to);

You can also pattern match it if you can't change 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);

Upvotes: 6

Denis Ermolin
Denis Ermolin

Reputation: 5546

You can typedef nodeType inside class definition:

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

Upvotes: 3

Related Questions