views:

265

answers:

8

I'm new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

+4  A: 

Of course they are used in the industry. Just look at the Standard Template Library (STL)!

You definitely should know how to work with templates, and better, learn how to do some fancy things with them like template metaprogramming, ok, that may be optional, but template classes are not!

EDIT: Even more, a lot of other languages like Java and C# support the concept of generics, that being the equivalent to templates in c++, but generics are far more restrictive.

Seth Illgard
Generics are not templates. They are far more restricted.
Thomas
It's the equivalent in the other languages. Thanks for the correction. English is not my native language so sometimes I don't know how to say things correctly.
Seth Illgard
Edited and corrected, thank you.
Seth Illgard
+5  A: 

At my employer, the main thing we use that's in C++ and not in C is indeed generics, AKA "templates". Inheritance &c are nice (but with pluses and minuses), but templates are absolutely crucial -- the main, cost-less advantage of C++ over plain C. Master them, you won't be wasting time!-)

Alex Martelli
Generics are not templates. Templates are not generics.
Thomas
Generics = C#. Templates = C++
Maciek
No they are not =. C++ are far more flexible, in fact C++ templates are turing-complete. Basically that means that you can write fully evaluable program using templates only (you can emulate all flow control instructions like ifs, loops etc). You can see them as "language in language".
doc
@doc: and that's the root of C++ meta-programming features!
Matthieu M.
Of course templates are for generic programming -- Austern's book "Generic Programming and the STL" was written before C# was even a gleam in Microsoft's eye! "Generic programming" is a programming paradigm (like, say, "functional programming"), "generics" are entities implementing it, and templates are C++'s generics.
Alex Martelli
+1 to Alex's comment. People get way too caught up in the specific name used for something like C#'s generics, and forget that it also is a word that can be used *generically*.
Jesse Beder
Alex: I see your point, but I'm not sure I agree. I'd say "generics" is the specific name for the constructs implemented in C#/Java. "Generic programming" is a much more general concept. I think it just gives people the wrong impression when you say templates are C++ generics. They're used for generic programming in C++, but they're not generics.
jalf
@jalf, what do you claim templates lack to be generics?! As http://en.wikipedia.org/wiki/Generic_programming says, "These "containers-of-type-T", commonly called generics, are a generic programming technique allowing a class to be reused with different datatypes as long as certain contracts such as subtypes and signature are kept." -- note, **commonly called** generics! You can't say C++ doesn't have generics just because it calls them templates, any more than you can say it doesn't have procedures just because it calls them functions!!!-)
Alex Martelli
I think Jalf's point is that referring to templates as C++ generics may lead to the impression that you are calling them the C++ equivalent of C# (or Java) Generics.
jon hanson
+3  A: 

In addition to what the others said, they can be incredibly useful in your own code, and you definitely will come across it while reading others', so there is no harm in learning it.

yodie
+1 - I'd even make it sharper: even if you don't need them, as a C++ programmer you need to understand them. Otherwise, it would be like "I do speak Spanish fluently, I just can't order food in a restaurant"
peterchen
+2  A: 

You cant go far without mastering templates in C++ if your writing code on windows. ATL, STL and various other windows libraries heavily use templates.

Side note*. If your using visual studio , you might want to learn the quirks which are used in visual studio to support templates. For example , many windows libraries often declare their entire contents in header files to work around linker bugs. Its annoying, but this limitation often is a stumbling block for newbie's learning C++ using visual studio.

Andrew Keith
+9  A: 

You'll need them sooner or later ;) But for a beginner they are a bit tricky to write (but easy to use, so use STL intensively). And after mastering other c++ skills (const-correctness, operators overloading, learning about compilation units etc) and gaining some experience you will start coding templates for yourself. So my advice: use them but learn how to write them when you'll find you need them. It will be much quicker and effective learning if you gain some experience with standard c++ constructs first.

doc
+1: there is a neat difference between using and creating. If the first is required, the second really depends on what one is trying to accomplish (and much more tricky).
Matthieu M.
+2  A: 

No, don't spend to much time on learning how to implement them. The other answers correctly explain that you certainly should learn how to use them. That makes sense; many libraries provide class templates and you should be learn to use those libraries. But writing your own templates is much rarer.

MSalters
It's a useful skill to have, but it isn't the first to learn.
David Thornley
Many things are not the "first to learn". But it depends what the original poster is trying to do. In other words, there's a difference between learning a language to have it under the list of languages you know, and learning a language to use it. If the original poster wants to do the latter, templates are important. If it is the former... not so much.
yodie
A: 

I'd say understanding templates is a requirement for modern C++ style and idioms and getting the most out of the language. They aren't as difficult as one is often made to believe. If you are a beginner I'd absolutely recommend a book like Accelerated C++ which offers a very gentle and natural introduction to templates.

BuschnicK
+1  A: 

As others have noted, Templates are a very important part of the C++ language. In fact, they're a turing-complete language themselves ;) However, not only does the STL make heavy use of templates (ever used std::vector or std::pair?), lots of other libraries do, too... the most notable is probably boost. I hope that gives you an impression on how important they are.

What you should definitely learn is how to use them in the context of classes and functions. When you mastered that, you can move on to more advanced topics like template specialization or template-template parameters. And if you still haven't got enough of templates, you can check out "C++ Templates: The Complete Guide" by Nicolai M. Josuttis and "Modern C++ Design" by Andrei Alexandrescu. Even though the latter is really a hard piece to understand.

For a start, this resource http://www.parashift.com/c++-faq-lite/templates.html might be helpful, too :)

Please also note that Generics aren't "Templates in C#/Java" or vice versa... they follow a different concept.

Christian
+1, although I'm not sure I would pick vector/pair as an example of "heavy use of templates" ;)
Idan K