Post Your C++ Templates.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Falco Girgis
Elysian Shadows Team
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.

Post by Falco Girgis »

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.
User avatar
dandymcgee
ES Beta Backer
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.

Post by dandymcgee »

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! :twisted:
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

Re: Post Your C++ Templates.

Post by trufun202 »

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:

Code: Select all

//setting
Session[playerID] = player;

//getting
Player player = (Player)Session[playerID];
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:

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;
        }
    ...
}
So now, I can do things like this:

Code: Select all

Player player = SessionHandler.Get<Player>(playerID);
string msg = SessionHandler.Get<string>("msg");
int number = SessionHandler.Get<int>("number");
-Chris

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
User avatar
Ginto8
ES Beta Backer
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.

Post by Ginto8 »

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.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Post Your C++ Templates.

Post by K-Bal »

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
User avatar
wtetzner
Chaos Rift Regular
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.

Post by wtetzner »

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?
I wouldn't call templates a fancy feature of C++ so much as a work-around for the limitations caused by static type checking.

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.
User avatar
Falco Girgis
Elysian Shadows Team
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.

Post by Falco Girgis »

wtetzner wrote:
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?
I wouldn't call templates a fancy feature of C++ so much as a work-around for the limitations caused by static type checking.

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);
Omg, you bastard!

That's exactly what I was about to write! A hash table ATD, great minds think alike! ;)
CC Ricers
Chaos Rift Regular
Chaos Rift Regular
Posts: 120
Joined: Sat Jan 24, 2009 1:36 am
Location: Chicago, IL

Re: Post Your C++ Templates.

Post by CC Ricers »

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.
fantastico
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Fri Mar 20, 2009 4:37 am
Location: Germany

Re: Post Your C++ Templates.

Post by fantastico »

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;
  }
}
Post Reply