Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 925
Patryk Avatar asked Nov 20 '12 14:11

Patryk


People also ask

What is template in C ++ types of templates?

What is Templates in C++? Templates in C++ is an interesting feature that is used for generic programming and templates in c++ is defined as a blueprint or formula for creating a generic class or a function. Simply put, you can create a single function or single class to work with different data types using templates.

What is a template argument C++?

A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function.

What is a template template parameter?

The Template Parameters tool is available for viewing template parameter usage in articles. It works with TemplateData to show the frequency of parameter name usage in a template's mainspace transclusions, along with whether or not each parameter is listed in that template's TemplateData code as a supported parameter.

What is parameterized template?

In UML models, template parameters are formal parameters that once bound to actual values, called template arguments, make templates usable model elements. You can use template parameters to create general definitions of particular types of template.


2 Answers

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);
like image 58
Pubby Avatar answered Oct 03 '22 10:10

Pubby


You can typedef nodeType inside class definition:

typedef typename T::nodeType TNode;
PathFinder<T>::getPath( TNode from, TNode to);
like image 33
Denis Ermolin Avatar answered Oct 03 '22 10:10

Denis Ermolin