Post Your C++ Templates.
Moderator: Coders of Rage
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Post Your C++ Templates.
I've been looking/reading/analyzing certain aspects of Object Oriented Programming as of late. As sinister as this may sound, I am really interested in seeing what applications all of you OO extremophiles have applied C++ template programming to.
As always, I'm interested in making code more vesatile, extendable, and functional, which is the goal of templates in general. Templates are also resolved at compile-time rather than run-time which basically invalidates the "omg efficiency" argument of all of you anti OOP-ers.
So why not use templates? Why not go all out? Because I am having trouble finding places where they are necessary, practical, or even beneficial. I'm trying to decide whether my days of straight C on the Dreamcast have hardcoded my brain into not recognizing all of the shining opportunities to exploit fancy features of the C++ language. Is that the case, or does the main power from template programming come in the form of ADTs (in which case we have STL, so why bother?), and OO finatics are just flaunting templates wherever they can use a function that can take a float or a double in order to feel intelligent?
Seriously. Show me code. Enlighten me.
As always, I'm interested in making code more vesatile, extendable, and functional, which is the goal of templates in general. Templates are also resolved at compile-time rather than run-time which basically invalidates the "omg efficiency" argument of all of you anti OOP-ers.
So why not use templates? Why not go all out? Because I am having trouble finding places where they are necessary, practical, or even beneficial. I'm trying to decide whether my days of straight C on the Dreamcast have hardcoded my brain into not recognizing all of the shining opportunities to exploit fancy features of the C++ language. Is that the case, or does the main power from template programming come in the form of ADTs (in which case we have STL, so why bother?), and OO finatics are just flaunting templates wherever they can use a function that can take a float or a double in order to feel intelligent?
Seriously. Show me code. Enlighten me.
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: Post Your C++ Templates.
I'm in the same boat as you man.. I haven't come across any practical use for them in what little programming experience I have, and as such know very little about their use. Let us share this discovery.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- trufun202
- Game Developer
- Posts: 1105
- Joined: Sun Sep 21, 2008 12:27 am
- Location: Dallas, TX
- Contact:
Re: Post Your C++ Templates.
I've used a few STL maps and lists in Golvellius, but that's about it.
But, in the .NET (and Java) world, templates have essentially morphed into Generics. Here's a simple (and powerful) use of Generics, which would probably translate well into templates in C++.
In web applications, you can store objects in Session. Doing so can be done directly using the current Session object that runs under HttpContext.
Such as:
This works, but what if Session[playerID] is null? You need to wrap it with some ifs and potentially handle this case in several sections of the code.
Instead, I created a SessionHandler which will allow me to gracefully fetch objects from Session:
So now, I can do things like this:
But, in the .NET (and Java) world, templates have essentially morphed into Generics. Here's a simple (and powerful) use of Generics, which would probably translate well into templates in C++.
In web applications, you can store objects in Session. Doing so can be done directly using the current Session object that runs under HttpContext.
Such as:
Code: Select all
//setting
Session[playerID] = player;
//getting
Player player = (Player)Session[playerID];
Instead, I created a SessionHandler which will allow me to gracefully fetch objects from Session:
Code: Select all
public static class SessionHandler
{
...
static public T Get<T>(string Key)
{
object holder = null;
if (HttpContext.Current.Session[Key] != null)
{
holder = HttpContext.Current.Session[Key];
}
else
return default(T);
if (!(holder is T))
throw new InvalidCastException("Could not cast from type: " + holder.GetType().ToString() + " to " + typeof(T).ToString());
return (T)holder;
}
...
}
Code: Select all
Player player = SessionHandler.Get<Player>(playerID);
string msg = SessionHandler.Get<string>("msg");
int number = SessionHandler.Get<int>("number");
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Post Your C++ Templates.
Really the only templates I have at the moment are for Rects and color structs, so that I don't have to make another class for a rect/etc. using floats if it uses ints by default
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Re: Post Your C++ Templates.
Ginto8 wrote:Really the only templates I have at the moment are for Rects and color structs, so that I don't have to make another class for a rect/etc. using floats if it uses ints by default
This and the STL seem to me the only useful application of templates, at the moment
Though, if I find another good use for them, I'll post it.
Ciao,
Marius
- wtetzner
- Chaos Rift Regular
- Posts: 159
- Joined: Wed Feb 18, 2009 6:43 pm
- Current Project: waterbear, GBA game + editor
- Favorite Gaming Platforms: Game Boy Advance
- Programming Language of Choice: OCaml
- Location: TX
- Contact:
Re: Post Your C++ Templates.
I wouldn't call templates a fancy feature of C++ so much as a work-around for the limitations caused by static type checking.GyroVorbis wrote: So why not use templates? Why not go all out? Because I am having trouble finding places where they are necessary, practical, or even beneficial. I'm trying to decide whether my days of straight C on the Dreamcast have hardcoded my brain into not recognizing all of the shining opportunities to exploit fancy features of the C++ language. Is that the case, or does the main power from template programming come in the form of ADTs (in which case we have STL, so why bother?), and OO finatics are just flaunting templates wherever they can use a function that can take a float or a double in order to feel intelligent?
Most of the uses I've found for templates are ADTs, and even though we have the STL, sometimes you need to make your own data structures. For example, I created a type called ObjectList, which basically stores objects in a linked list, but at the same time it has a hash table containing pointers to the elements of the list. This way I can sort and iterate through the objects for drawing purposes, but they can also be quickly referenced by name.
Code: Select all
ObjectList<Sprite> objects;
Sprite character;
objects.Set("Fred", character);
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Post Your C++ Templates.
Omg, you bastard!wtetzner wrote:I wouldn't call templates a fancy feature of C++ so much as a work-around for the limitations caused by static type checking.GyroVorbis wrote: So why not use templates? Why not go all out? Because I am having trouble finding places where they are necessary, practical, or even beneficial. I'm trying to decide whether my days of straight C on the Dreamcast have hardcoded my brain into not recognizing all of the shining opportunities to exploit fancy features of the C++ language. Is that the case, or does the main power from template programming come in the form of ADTs (in which case we have STL, so why bother?), and OO finatics are just flaunting templates wherever they can use a function that can take a float or a double in order to feel intelligent?
Most of the uses I've found for templates are ADTs, and even though we have the STL, sometimes you need to make your own data structures. For example, I created a type called ObjectList, which basically stores objects in a linked list, but at the same time it has a hash table containing pointers to the elements of the list. This way I can sort and iterate through the objects for drawing purposes, but they can also be quickly referenced by name.Code: Select all
ObjectList<Sprite> objects; Sprite character; objects.Set("Fred", character);
That's exactly what I was about to write! A hash table ATD, great minds think alike!
Re: Post Your C++ Templates.
I rarely write my own templates, but right now I am writing one to take inputs and outputs for variables of different types whenever a program has to get immediate feedback from input devices.
-
- Chaos Rift Newbie
- Posts: 20
- Joined: Fri Mar 20, 2009 4:37 am
- Location: Germany
Re: Post Your C++ Templates.
generic insertion sort I used in a situation where I knew an array was partly sorted already, cmpFnc has to be a class/struct with an operator() that realizes a "strictly less than" comparison
Code: Select all
template <class T, class ComparisonFunctor>
void insertion_sort(std::vector<T>& arr, ComparisonFunctor cmpFnc)
{
for (std::size_t i = 1; i < arr.size(); i++)
{
T val = arr[i];
std::size_t j = i;
while (j > 0 && !cmpFnc(arr[j - 1], val))
{
arr[j] = arr[j - 1];
j--;
}
arr[j] = val;
}
}