gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > Will I ever learn what a "template" is?

#53559 - LOst? - Fri Sep 09, 2005 4:57 am

Okay, seriously the "template" keyword has scared me for some time now. Everytime i try to understand it by looking at it, it makes less sence than ever before. Is it some kind of dynamic type macro? Is it really needed in the real word of programming? Is there anyone that can show me the most simple template ever made, and explain how you could have written it without using the template and get the same result?
_________________
Exceptions are fun

#53572 - poslundc - Fri Sep 09, 2005 8:25 am

There's nothing very helpful or substantial I could write at this hour, but I will direct you to these two documents, which explain the subject better than I could anyway:

http://www.cplusplus.com/doc/tutorial/tut5-1.html
http://www.parashift.com/c++-faq-lite/templates.html

The first will teach you about templates, and the second should answer most of the questions you'll have about them.

The basic idea is not unlike macro expansion. Wherever you see an instantiation of a template, look at the data type (or whatever, but usually data type or class) and imagine that the entire original template has been copied into the code with whatever is instantiated replicated throughout the code. That's not exactly what the compiler does, but it's close enough.

Dan.

#53608 - sajiimori - Fri Sep 09, 2005 6:58 pm

Those documents seem to cover it, except the question of whether they are ever needed in the real world. Since "need" is hard to define, I'll just say that they can be very helpful.

The STL is perhaps the most prominent example of a real-world case where templates offer substantial benefits. STL is part of the C++ standard library. It's primarily a set of containers that can hold anything, and algorithms that can work on those containers.

Also check out std::auto_ptr and the entire Boost library for tons of other template applications.

#57809 - legalize - Tue Oct 18, 2005 5:51 pm

Templates are most definately needed in the real world.

Ever write a handfull of functions that are all the same except for the data type that they accept/emit? Then you just demonstrated the need for a template function.

#57816 - poslundc - Tue Oct 18, 2005 6:47 pm

legalize wrote:
Templates are most definately needed in the real world.

Ever write a handfull of functions that are all the same except for the data type that they accept/emit? Then you just demonstrated the need for a template function.


I would replace "need" with "useful" in all instances there.

In a system of limited resources such as the GBA, templates are something to be approached with caution as they can bloat code and lead to overengineered solutions.

Dan.

#57941 - sgeos - Wed Oct 19, 2005 1:21 pm

sajiimori wrote:
Those documents seem to cover it, except the question of whether they are ever needed in the real world. Since "need" is hard to define, I'll just say that they can be very helpful.

Anything you want to do can be done by directly writing machine code. Nobody does this anymore for various obvious reasons.

Yes, they are "needed" as much as anything else is.

-Brendan

#57979 - legalize - Wed Oct 19, 2005 7:25 pm

poslundc wrote:
In a system of limited resources such as the GBA...


I was answering the question generally. There are many C++ language constructs that may not be appropriate for a limited resource environment. Hell, even malloc may be inappropriate.

However, templates give you type generality while also giving you type safety. If you're writing a large C++ project for the GBA this will still be appropriate on occasion.

Like any other tool, it can be misused and abused. You don't dig a hole for a tomato plant with a nuclear bomb. You use the right tool for the job and its not a problem, not even on the GBA.

#58040 - sgeos - Thu Oct 20, 2005 12:01 pm

I think a list of C, C++ and ARM tutorials should be added to the FAQ.

-Brendan

#58064 - tepples - Thu Oct 20, 2005 4:49 pm

PM me some ones that you think are good (not just "use Google" unless it's search terms for use with "I'm Feeling Lucky"), and I'll add them.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#59233 - jun - Sun Oct 30, 2005 4:36 pm

C++ alone can be overkill for GBA development let alone templates.

#59260 - tepples - Sun Oct 30, 2005 8:05 pm

jun wrote:
C++ alone can be overkill for GBA development let alone templates.

Only those features of C++ that your program actually uses get compiled into the ROM. By "C++" in this case, did you mean virtual methods?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#59284 - jun - Mon Oct 31, 2005 2:54 am

correct tepples, by C++, i mean the class derivations and inheritances :) C++ can be misleading. others might tend to think that main.c is "faster" than main.cpp, regardleas of the contents. :)

#59336 - legalize - Mon Oct 31, 2005 6:36 pm

jun wrote:
correct tepples, by C++, i mean the class derivations and inheritances :)


Class derivation and inheritance is not the same thing as virtual methods. Even virtual methods may be appropriate on the GBA; it depends on the program. Virtual methods are just indirect function calls with the function pointer managed by the compiler.

#59347 - tepples - Mon Oct 31, 2005 7:37 pm

legalize wrote:
jun wrote:
correct tepples, by C++, i mean the class derivations and inheritances :)

Class derivation and inheritance is not the same thing as virtual methods.

Inheritance without anything virtual is implemented in terms of encapsulation, which isn't generally thought of as a "C++" feature. The ++ stands for polymorphism.

Quote:
Even virtual methods may be appropriate on the GBA; it depends on the program.

Which is why jun said that C++ can be overkill.

There are rules of thumb (virtual method calls inside a loop tend to be expensive), and then there are tradeoffs for breaking the rules of thumb. If you can make the tradeoffs work within 280,000 CPU cycles, then go for it.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.