Get template, template type

I create a small โ€œgeneralโ€ path to define a path that takes the type of the Board class where it will find the paths,

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

While Board also has a template for storing node type. (so that I can find paths in two-dimensional or three-dimensional vector spaces).

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

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

How can I perform type compatibility for a node T type and nodeType , which is passed to the function as a parameter?

+4
source share
2 answers

If I understand what you want, specify a board type member and use this:

 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 map a template if you cannot change the 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); 
+6
source

You can define typedef nodeType inside the class:

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

Source: https://habr.com/ru/post/1447237/


All Articles