Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template class type-specific functions

Ok, so i have this template class, which is kinda like one-way list.

template <typename T> List

and it have this inside function print

public:
void Print();

which, as you can guess, prints the list contents from begining to end; However, as template can take classes as T, one can imagine, that i would need different implementations of Print() for that very cases. For example, I have another class Point

class Point{
 private:
  int x, y;
 public:
  int getX();
  int getY();
}

so i want Print specifically designed for Points. I tried this:

void List<Point>::Print();

but compiler tells me

prototype for void List<Point> Print() doesn match any in class List<Point>

though

candidates are: from List<T> [with T = Point] void List<Point>::Print()

For me it seems like the same fucntion. What's wrong? And how do I write T-specific template class functions?

like image 901
Yury Elburikh Avatar asked Nov 21 '16 14:11

Yury Elburikh


People also ask

Can templates be used for functions?

Templates are powerful features of C++ which allows us to write generic programs. We can create a single function to work with different data types by using a template.

How will you restrict the template for a specific datatype?

There are ways to restrict the types you can use inside a template you write by using specific typedefs inside your template. This will ensure that the compilation of the template specialisation for a type that does not include that particular typedef will fail, so you can selectively support/not support certain types.

What is class and function template?

A template allows us to create a family of classes or family of functions to handle different data types. Template classes and functions eliminate the code duplication of different data types and thus makes the development easier and faster. Multiple parameters can be used in both class and function template.

How is a template class different from a template function?

For normal code, you would use a class template when you want to create a class that is parameterised by a type, and a function template when you want to create a function that can operate on many different types.


2 Answers

You use explicit template specialization to specialize behaviour of Print for specific types.

For example, for Point:

template <> // No template arguments here !
void List<Point>::Print() // explicitly name what type to specialize
{
  //code for specific Point Print behaviour..
}
like image 195
Hatted Rooster Avatar answered Oct 26 '22 20:10

Hatted Rooster


However, as template can take classes as T, one can imagine, that i would need different implementations of Print() for that very cases

Not at all. You can have a single implementation of Print for every type of object - this is why templates are powerful.

One way to do what you want would be to define the stream operator << in Point, and have a generic Print() method in List. This makes Print available to more than just Point.

More generality ftw.

like image 36
aspen100 Avatar answered Oct 26 '22 21:10

aspen100