Help me understand templates.

So I've been following along with this guys (link below) playlist on C++ and SDL2 and have had no issues until now. Studying C++ on my own time for a few years now, I've got a pretty firm grasp on the basics, up to things like classes, pointers, inheritance, polymorphism. However I've never understood templates and at this point in the series he introduces them, at a rather crucial part, the entity component system. I Love the way it functions as I can attach different functionality to different objects I've created, it's brilliant.

However, I DON"T UNDERSTAND IT! Could someone try to help me wrap my head around what's going on here? Mostly the template stuff he gets into? If you have a good tutorial or resource that helped YOU understand them better or something that just made it click, feel free to share. I really want to understand what is going on here, and not just copy/paste his code and move on.

It's bugging me to the end of the world and back that I can't grasp what's going on here.

(TEMPALTE STUFF STARTS ABOUT 4:30 INTO VIDEO)
VIDEO LINK: https://www.youtube.com/watch?v=XsvI8Sng6dk
Last edited on
like most concepts, start with a smaller, simpler example.

you probably use templates all the time:

vector <int> x; //this is a template that is now set for integers
vector<myclass> y; //this is a template that now uses your custom class.

so a template, in its most basic form, is just what the word implies. It is an incomplete class that is completed when you provide it with a concrete type. At compile time, the compiler generates a normal class from the provided type. That is, if you had this silly, simple template:

template class example <type t>
{
type * tp;
};

and called it with

example <int>
the compiler generates

class example <int t>
{
int * tp;
};

and if you later have another version with example<myclass> x it will generate another, different class from the template.

Once you get your head around that, you can go into deeper examples where you can specialize the template so that if the provided type is one thing, do this, if another, do that, to handle things that won't work properly if the wrong type is provided (eg if your class divided by the type provided and the user provided type string, you might convert the string to some numeric before dividing as a specialty). All the same things you know about classes and polymorphism and inheritance etc still apply for the most part.

One oddness of templates is the entire class body is normally in a .h file.

I prefer to read... I learned about templates in my c++ book, though they were not covered in my classes (hah), or if so, only briefly. OOP had not quite caught on yet when I was in school.






closed account (E0p9LyTq)
A good start to learn templates would be the tutorial section here on templates:
http://www.cplusplus.com/doc/oldtutorial/templates/
Both that video and the tutorial that FurryGuy did link do start with function templates.

You surely know function overloading?

I could write:
1
2
3
void foo(int);
void foo(double);
void foo(std::string);

Different functions.

What if each of them does the same operations and the only thing that differs is the type of the argument? (This obviously requires that those operations call similarly overloaded functions/operators.)

It gets boring and error-prone to copy-paste-edit (and maintain) overloads for multiple types, does it not?

Furthermore, what if someone else uses my functions and has a new type for which no overload exist? Have you ever written a class or struct and then stored some instances in std::vector? Did the library writers knew that you would, or did you write a version of std::vector for your type?


What the video starts with is ... interesting:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int bar() {
  static int x = 0;
  return x++;
}
// every call of bar() returns a different number

template <typename T>
int foo() {
  static int y = bar();
  return y;
}

int main() {
  std::cout << foo<int>() << '\n';
  std::cout << foo<float>() << '\n';
  std::cout << foo<int>() << '\n';
  std::cout << foo<double>() << '\n';
}

The program has one bar(), but three "kind of overloaded" functions:
1
2
3
int foo<int>();
int foo<float>();
int foo<double>();

The compiler wrote their implementations for us.
@Keskiverto that explanation makes the most sense I've heard of so far. Thanks! I'll keep studying up on this stuff but it's deff. taking me more time to digest "templates" than it has anything else in regards to C++
Topic archived. No new replies allowed.