Template classes

Started by
26 comments, last by Jiia 18 years, 11 months ago
Quote:Original post by d000hg
Oh, OK. I thought Templated classes were just special classes. So actually
they're a dirty hack? If I define the function body in the class declaration
are they still inline functions - I probably don't want that.

actually they are a powerful tool that many languages would die to have
available, and also opened possibilities leaps and bounds ahead of their time.
notice how Java 1.5 has introduced "generics" ?

your second point: the compiler is a wicked-smart peice of software, so you
shouldn't worry about speed unless you're planning on some uber-cryptic code,
if it's just because you don't like the layout you can implement your code and
save it as a ".inl" file (any extension you like, commonly .inl) and at the
bottom of the .h file you include the file: #include "MyTemplateImpl.inl"

that tends to work quite well in most situations (and I'm unaware of those
for which it wont).

Cheers
-Danu
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Advertisement
Quote:Original post by d000hg
If I define the function body in the class declaration are they still inline functions - I probably don't want that.

If your functions are simple one-liners, there's very little reason to not inline them. Your code must either call a function in that section (non-inlined), or it must execute the line of code (inlined). The bloating of inlining is virtually non-existent when the code itself contains a less or equal number of operations than the function call, as in your examples.

As stated above, the in-class declaration is just a suggestion anyways. Your compiler will be smart enough to sort it out for you.

edit: Just out of curiousity, does anyone know what module the compiler would put header code in? That is, code that was chosen not to be inlined? Does it have a super secret *obj file stashed somewhere? [wink]
The code is placed in every object file that uses it. The duplicate code is then eliminated during linking.
d000hg if you wasn't aware then the c++ standard library provides a set of containers, one of which is a C++ dynamic array known as std::vector you should look that up and use that instead, two references here and here
What has this got to do with my post?
Quote:Original post by snk_kid
d000hg if you wasn't aware then the c++ standard library provides a set of containers, one of which is a C++ dynamic array known as std::vector you should look that up and use that instead, two references here and here


There are tons of ways to utilize templates besides reinventing STL objects. But even if that's what he's doing, you're basically telling him to avoid learning how it works.
Quote:Original post by d000hg
What has this got to do with my post?


It has a lot to do with it; you should avoid using C-style dynamic arrays in C++, use C++ dynamic arrays std::vector.

Quote:Original post by Jiia
There are tons of ways to utilize templates besides reinventing STL objects.


What does that have anything to do with what I said and his code, it is obvious he using a dynamic array.

Quote:Original post by Jiia
But even if that's what he's doing, you're basically telling him to avoid learning how it works.


Obviously, you do not have a clue, and no he is not going to learn how compiler vendors implement the standard library vector, as it is not as simple as you may think it is. I am feed up of your sly/rude comments.

[Edited by - snk_kid on May 25, 2005 3:53:41 AM]
Quote:Original post by snk_kid
Quote:Original post by Jiia
There are tons of ways to utilize templates besides reinventing STL objects.

What does that have anything to do with what I said and his code, it is obvious he using a dynamic array.

I expect the code he pasted was an example. The name of his class being "MyClass" kind of gave it away for me. He's interested in understanding how to use templates. Not in how to avoid writing them.

Quote:
Quote:Original post by Jiia
But even if that's what he's doing, you're basically telling him to avoid learning how it works.

Obviously, you do not have a clue, and no he is not going to learn how compiler vendors implement the standard library vector, as it is not as simple as you may think it is. I am feed up of your sly/rude comments.

I don't have a clue about what? I don't think this thread has anything to do with vectors.

[Edited by - Jiia on May 25, 2005 5:56:53 AM]
I thought I would clarify why you should use std::vector:

(Unless it has been left out) your code does not implement deep copy semantics, the default copy and assignment operations implicitly defined does a member-wise copy meaning you will end up with two or more instances of your class referring to the same array and eventually calling delete twice or more on it, bad news.

Simply invoking array new like that is naïve and inefficient for large instances as all elements are default constructed if there even is a default constructor defined for the type, in which case you cannot use types that do not have default constructors also you cannot even use non-default construction like std::vector can.

To do this efficiently is very low-level and advance C++ but std::vector takes care of all this for you because it separates allocation/de-allocation and construction/destruction. It allows you to reserve a chunk of uninitialized memory then incrementally initialize elements, which is what you want to do instead of creating 100 VERTEX default constructed.

std::vector is also parameterized by allocator type so you can use custom allocator types with different memory scheme/models, your class does not allow for this easily, forcing yourself or clients to redefine or overload operators new/delete to use custom memory management.

It is redundant to check for null before invoking delete/delete[] as pointers are permitted to be null when given to delete/delete[].

You will end-up rewriting mediocre routines such as growing; std::vector takes care of this, if your not doing this to learn your simply wasting your time.
Quote:Original post by Jiia
I expect the code he pasted was an example. The name of his class being "MyClass" kind of gave it away for me. He's interested in understanding how to use templates. Not in how to avoid writing them.


I am not telling him to not use templates. I am suggesting on using the standard library vector instead of C-style dynamic arrays, which is good advice.

This topic is closed to new replies.

Advertisement