🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Template linked lists

Started by
2 comments, last by Qoy 24 years, 5 months ago
I am implementing a linked list for my map functions, and I''m gonna need to use it for two or three different types. So I have started implementing the list functions in template functions. Everything seems to work fine, but I was wondering, what kind of overhead is involved with templates? Will this significantly slow down my code? Or is there just overhead involving executable side? The main question is, is there anything to worry about, or should I use some other method? I like the template method, but if it''s gonna impact my code, then I would rather not use it... ------------------------------ Jonathan Little invader@hushmail.com http://www.crosswinds.net/~uselessknowledge
Advertisement
From what I know, templates are really just fancy macros. All the work is done at compile time, so you shouldn't have any real executable overhead.

Recently, I did some work trying to build a link list template. I eventually just went over to using the linked list with a base class and deriving all my linkable objects from that class. Just a suggestion, but I found it easier to deal with the pointers in the objects that way.

Edited by - I-Shaolin on 1/14/00 1:23:07 AM
Template implementation isn''t specified by the language. Older versions of g++ spit out some really ugly code for templates, but most current compilers treat them pretty much as macros as I-Shaolin stated.
'k, I just learned STL a couple of months ago, so I've researched this a bit:

- Templates do not affect run-time speed. When you created a templated instance of a class, it is just as if you had written all the code by hand for that class. In this case, it's like a macro. The effeciency of the code depends on the code itself (i.e. how well you wrote it), and has nothing to do with the fact that it's a template.

- Templates DO affect compile-time and program size. For every different type you template, the compiler generates all the functions for that type. So if you have one templated function:

template < class T >
bool lessThan (const &T lhs, const &T rhs)
{ return lhs < rhs; }

...and use 5 different types with it (int, double, class C, whatever), 5 different copies of the code for lessThan will be generated. If you have HUGE class templates, this can seriously bloat the size of your program.

- A templated linked list is available for use in STL. It's pretty efficient, as almost all of its functions are inline. And once you learn one STL container type, you pretty much know them all (vector, deque, list, set, map).

STL: learn it, loathe it, live it.

Edited by - Stoffel on 1/14/00 11:58:37 AM

This topic is closed to new replies.

Advertisement