Page 1 of 1

Post Your C++ Templates.

Posted: Mon May 11, 2009 3:33 pm
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.

Re: Post Your C++ Templates.

Posted: Mon May 11, 2009 3:53 pm
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.

Re: Post Your C++ Templates.

Posted: Mon May 11, 2009 4:04 pm
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");

Re: Post Your C++ Templates.

Posted: Mon May 11, 2009 8:19 pm
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 ;)

Re: Post Your C++ Templates.

Posted: Tue May 12, 2009 2:10 am
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

Re: Post Your C++ Templates.

Posted: Tue May 12, 2009 4:05 pm
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);

Re: Post Your C++ Templates.

Posted: Tue May 12, 2009 4:20 pm
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! ;)

Re: Post Your C++ Templates.

Posted: Tue May 12, 2009 9:14 pm
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.

Re: Post Your C++ Templates.

Posted: Sun May 17, 2009 3:41 pm
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;
  }
}