Templates

Started by
55 comments, last by CoffeeMug 19 years, 7 months ago
Do you use them? How deep is your understanding of them? Do your team members/friends/co-workers use them? I started learning about them fairly recently and so far they have improved my design tremendously. When used judiciously they can completely transform C++ design into a whole new realm (that's generally one step closer to heaven). I've been intimidated by templates for quite some time. After taking a little time to learn about them, I have to say that they're certainly not as complicated as I thought (at least on a superficial level). It seems to me that they're still largely taboo for C++ programmers. I know *extremely* talanted people that work for very high quality teams on high budget projects where templates are banned (and most programmers on the team seem to agree with the ban). I find it rather interesting how 120k+ C++ programmers have so little knowledge about templates (and are consequently afraid of using them). Is it because until recently compilers had very poor support for them? Is it because it's a relatively new feature and people didn't get the chance to learn about it? I wish people took some time to learn about templates because they don't know what they're missing. It's sad how so many talanted people avoid this excellent feature of the language for reasons they themselves don't understand.
Advertisement
I want templated typedefs, damnit!
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
I use them in my own code, they are frowned upon a little at work. Probably because 90% of the code cycle is maintaining the code, and it is expensive to train people in all the details of template meta-programming and template instantiation and overload rules.

I can't say i have ever missed them at work. Instead, for example, of using a generic matrix class at work, we simply use a class that is hardcoded for float, how often do you use anything other than float in a matrix class? Not very. Instead of using a dot product meta-program to unroll the loop of dot product, its done by hand.

The argument has also been put at work that they can bloat code with hidden instantiations, though if used properly with a good compiler, that can be reduced a lot. The trouble is the 'used properly' bit, that requires training people to use them properly, which requires time and effort.

Templates do have a lot of powerful uses though, and if the case arose where templates would make the code a lot more maintainable, then I don't think anyone could blame anyone at work for using them.

Personally, I am trying to cut down on the amount that I use templates now, if you are not careful you can end up with every function being a template, and all your code in header files, and 90% of the time it is completely pointless in it being like that.

Tons of template instantiations can also increase compile time dependencies and compilation time, which can be harmful to productivity.
Quote:Original post by smart_idiot
I want templated typedefs, damnit!


There there smart_idiot (patting his shoulder) that day will come.

CoffeeMug:

i would say in the past probably lack/crappy compiler support by particular compiler vendors (not naming culprets of course), and that meta-template programming can look real messy in the wrong hands and scary to novices. I would also blame some older books on c++ they tend to go about teaching c++ the wrong way/backwards even.

I guess another factor is c++ was only standardized in 1998.

To open up your eyes to whats possible get Mordern C++ Design
I am writing a Win32 class library as a side project (from my main project) and find that I can use templates almost everywhere, from streams to sockets to conversion functions. I love them.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Quote:Original post by snk_kid
To open up your eyes to whats possible get Mordern C++ Design

I've read this book more than once. Essentially, this book is the main reason why I started looking into templates in more detail.
Templates own. After using Python and other dynamically-typed language where classes are objects themselves (and thus making factories is simplicity itself) - if you're not using templates, you're not using C++. Java and C# completely failed to get my attention until they got generics.

They look scary (all the angle-brackets) but they're actually very easy once you learn the rules. At the very least, use the STL extensively. Code them yourself when you feel ready.
-- Single player is masturbation.
Quote:Original post by CoffeeMug
Do you use them? How deep is your understanding of them? Do your team members/friends/co-workers use them?

It's rare that I start a project anymore that doesn't have a large amount of templating. People you work with you who also understand the intricacies of templating will love it and those who don't understand templating will hate you for it or call it a waste of time.

Quote:Original post by CoffeeMug
It seems to me that they're still largely taboo for C++ programmers. I know *extremely* talanted people that work for very high quality teams on high budget projects where templates are banned (and most programmers on the team seem to agree with the ban).

Ouch. I would hate to work there. But I'm assuming they're not allowing templating because they focus on portability between compilers and since even now only a few compilers are even close to being fully compliant with it, it's understandable that they might make such harsh rules (though banning them completely still seems absurd to me -- as that means not only can you not make templated stuff but you can't even use the standard template libraries). Nowadays just about all new compilers are compliant enough that you can use basic templating with explicit and even partial specialization without problems.

Quote:Original post by CoffeeMug
I find it rather interesting how 120k+ C++ programmers have so little knowledge about templates (and are consequently afraid of using them). Is it because until recently compilers had very poor support for them? Is it because it's a relatively new feature and people didn't get the chance to learn about it?

I think it's all of those things, but mostly I think it's just that people look at templating as something that will take forever for them to understand and so they are turned off. There are a lot of advanced things you can do with templating that can be very intimidating to those who are new to the concept (metaprogramming, expressions templates, selection and exclusions of instantiations with SFINAE, etc.). It's like an entire new language once you really get into it.
That was me. Coulda sworn I put in my name :/ There should be a warning or something. O snap, now I can't even edit my typos.
Quote:Original post by Pxtl
Java and C# completely failed to get my attention until they got generics.


I wouldn't hold your breath, okay least C# do it better than java.

I do hold my breath for whats in store in C++/CLI AKA "The New C++" or as somebody once doubed microsoft's forked version of standard C++ lol, the combination of generics & templates...

STL.NET anyone?

This topic is closed to new replies.

Advertisement