is template programming necessary

Hi, I've been learning C++ for just over half a year now, and I've only touched the basics of template programming.
I feel pretty comfortable with "normal" c++ programming, but template programming seems to be an entire realm of its own, with unpleasantly unintuitive concepts that frankly, I feel are a waste of time for me.

So, my question is:
For someone who mainly wants to eventually develop small-scope games, web apps, perhaps some mobile apps, ect, is a strong knowledge of template programming necessary? Can I get along just fine without it?

Thanks :)
You can get along without them, yes.

But really they're not that complicated. They just let you write functions/classes in a way that can be used with different types instead of only working with 1 type.

Here's a simple example:

1
2
3
4
5
6
// a dumb function that triples a value passed by reference

void triple(int& v)
{
  v *= 3;
}


Simple enough, but the problem is it only works with ints. What if you have other types?

1
2
3
4
5
6
7
8
9
10
int main()
{
  int i = 1;
  double d = 1;
  short s = 1;

  triple(i);  // works OK
  triple(d);  // ERROR
  triple(s);  // ERROR
}


To get around this problem without templates, you have to rewrite (or copy/paste) that function for all the different types you want to use.

With templates, you can just make it a template:

1
2
3
4
5
template <typename T>
void triple(T& v)
{
  v *= 3;
}


Now it'll work with any of those above types.


It's really very simple.
Yes, it is very simple.
I'm talking about big boy templating.

Custom template containers with custom template iterators kind of templating.
Throw in endless inheritance, traits, unpleasant pointers that require spec templates, and suddenly its not so simple.

This is the kind of template programming that I'm wanting to avoid.
Sorry I didn't explain that well enough in the original post.
What do you think now?
Of course it's not necessary. None of it is necessary. You can do the whole thing in hand-crafted assembly. You don't need any C++ code at all.

However, just because you don't have a use for something doesn't mean that other people don't have a use for it. Some people do big boy templating and find it very handy. If you don't, then don't do it. Simple as that.
You can throw in all kinds of weird stuff without templates - pointers to pointers to pointers, mutual inheritance schemes, singletons, etc.

All templates do is provide a pattern (or "template") for the compiler to use to create something.

Instead of writing three functions to do something with different types, you only need provide the code once, and the compiler will create a function for every different type you give it.

That's the whole point of template programming -- having to write less code. But of course it can get hairy, though... as can any other programming task.
Yeah thats the part of template that I don't like. The hairy bits.
There are all kinds of crazy rules you have to learn, usually some kind of pre-instantiation technique for every template idiom.
I figure I'll eventually learn templates, but I made the mistake of trying to learn them thoroughly at the same time I'm learning just plain C++, resulting in massive headaches.

Thanks for the responses.
There's no point forcing it, you can do fine with just the basics of template programming.
You'll pick up the rest automatically as you go along (while reading code of others or while trying to build some fancy template solution for something).
Topic archived. No new replies allowed.