Templates?

Started by
6 comments, last by Nefrugle 20 years, 11 months ago
Hello, and thank you in advance. I am still learning C++ but I just dont understand Templates very well. I Got Classes and Error handling and all that stuff. But i don''t know why im having a hard time with this. Does anyone know of any good tutorials or anything for it. I have searched GameDev quickly but they only have like i think 1 article or something like that. I am just looking for a wider range for more thoughts and what not on this. Thanks for your Help NeFrUgLe
-- NeFrUgLe
Advertisement
the T means a variable-type.

class vector< type* T >;


vector vChar;
now you have a vector of char''s

vector vInt;
now you have a vector of int''s

vector< myStruct > vStruct;
now you have a vector of myStruct''s

See?

.lick
quote:Original post by Pipo DeClown
...

Changed Pipo DeClown''s code to be able to read it:

template <typename T>
class vector
{
...
};

vector<char> vChar; // now you have a vector of char''s
vector<int> vInt; // now you have a vector of int''s
vector<myStruct> vStruct; // now you have a vector of myStruct''s



Update GameDev.net system time campaign - success at last
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Maybe some very simlpe examples will help:

Let's say, for instance, that you make an integer (int) swap function for your program:


void myIntSwap(int& var1, int& var2)
{
int temp = var1;
var1 = var2;
var2 = temp;
}


Then you realise that you also want to be able to swap objects of type double, so you write a separate function for doubles:


void myDoubleSwap(double& var1, double& var2)
{
double temp = var1;
var1 = var2;
var2 = temp;
}


This function does exactly the same operations on the objects as myIntSwap(), it just does the operations on a different type.

Your program has to also swap a number of other built-in types and user-defined types, and you end up with 10 functions that do basically the same thing.

With the use of templates, you can make one generic function that allows you to not only swap the 10 types that you are already using in your program, but any more you wish to add:


template <typename T>
void mySwap(T& var1, T& var2)
{
T temp = var1;
var1 = var2;
var2 = temp;
}


Now you can use mySwap() like so:


int integer1 = 10, integer2 = 20;
mySwap(integer1, integer2); // mySwap(int, int);

double double1 = 5.6, double2 = 7.2;
mySwap(double1, double2); // mySwap(double, double);

char character1 = 'a', character2 = 'z';
mySwap(character1, character2); // mySwap(char, char);

CVector vector1(x1, y1, z1), vector2(x2, y2, z2);
mySwap(vector1, vector2); // mySwap(CVector, CVector);

CVehicle vehicle1(...), vehicle2(...);
mySwap(vehicle1, vehicle2); mySwap(CVehicle, CVehicle);


and it basically replaces T with the variable type. This is a very simple example, but it represents a situation of when you might use it, which usually helps people to understand.

Using templates to make generic classes is also a very useful and does too make for more usable/cleaner/better code. As the other posters sort of touched on, std::vector is a good example. Have a look on Google for more information, or, of course, buy a good reference book.

[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on May 4, 2003 7:14:37 AM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
I had a problem when I was first introduced to templates in which I couldn''t compile my code involving the use of the template (because the template was not ''defined'' to use that particular data type). The solution was to either put all the template code in the header file, or to declare the template with the data type you wanted to use if with (although I''m still unsure where you''re supposed to declare them in the latter).
Just a point I thought was worth mentioning.. I''m still kind of new to template classes so if I''m wrong or there''s a better way..
Templates can be a useful, but they can be a mess too. If you can''t ''get'' templates, you really don''t need to use them. For me, they are probably the feature of C++ I use the least (though i do use STL). I guess that doesn''t help you with your problem, but don''t kill yourself over templates.
quote:Original post by LaXe
I had a problem when I was first introduced to templates in which I couldn''t compile my code involving the use of the template (because the template was not ''defined'' to use that particular data type). The solution was to either put all the template code in the header file, or to declare the template with the data type you wanted to use if with (although I''m still unsure where you''re supposed to declare them in the latter).

The main problem with templates, I think, is that they are generally not very well supported. Visual C++ 6.0, still in widespread use among GameDev users (by me, for instance) is notorious for supporting them very poorly (partially remedied in later versions, but not fully). Even compilers that are generally very standards compliant (such as GCC/g++) lack some of the trickier features. For example, the C++ standard does allow for separate compilation of templates (that is, separating templated code into headers and implementation source files as with ordinary code). However, because of the way templates work, this requires the use of the keyword export (the functions in the implementation source file must be declared as exported functions); to the best of my knowledge, only one C++ compiler (Comeau C++) supports this.
Thanks for the Help and I am going to search Google. But for now I have sort of skipped templates and will come back to them.

Lektrix - That example really helped a little bit. Thank you for taking the time to post.

Everyone else - Also thanks for your advice/ expieriences on this subject.


NeFrUgLe
-- NeFrUgLe

This topic is closed to new replies.

Advertisement