views:

329

answers:

11

Some of the disadvantages would be

  1. its syntax is complex
  2. compiler generates extra code
+16  A: 

Hmm, how about...

3: They can be slow to compile

4: They force things to be calculated at compile time rather than run time (this can also be an advantage, if you prefer fast execution speed over runtime flexibility)

5: Older C++ compilers don't handle them, or don't handle them correctly

6: The error messages that they generate when you don't get the code right can be nearly incomprehensible

Jeremy Friesner
I disagree with 4. Use templates when appropriate, if you need run-time flexibility, don't use a template. 5 shouldn't matter except in old code bases where complaining about a language doesn't make sense. Modern compilers are either free or cheap. 6 is iffy. :P Understand templates and the errors are understandable.
GMan
Well, he asked for any disadvantages, not only insurmountable disadvantages :)
Jeremy Friesner
@GMan: I disagree with your POV re #6. For the innocent looking `std::list<int> l; std::sort(l.begin(),l.end());` VC9 spits a 7.5kB error message at you, all of which point into std lib headers. Even if I fully understand why sorting a list that way doesn't work, 7.5kB of error messages are far too much to understand.
sbi
Anyway, +1 from me, even though I would start this list with #2, since anish's #2 is bogus.
sbi
@sbi: Eh, tested it and the errors are still completely understandable. I guess for me understandable means you can understand what caused the errors, and for you it means the same thing but in a concise manner. :) But a +1 from me too, probably the best list.
GMan
@GMan: It's 28 error messages here, _one_ of which points to my code, and that says `see reference to function template instantiation 'void std::sort<std::list<_Ty>::_Iterator<_Secure_validation>>(_RanIt,_RanIt)' being compiled` - which is a _very_ warped version of what I invoked. You should see how humble fellow-workers approach my desk when they run into one of these. And the first view is always at the code they wrote, because understanding what went wrong from looking at your own code (_not_ the error messages it causes) usually is your best bet. __And that's just plain wrong__.
sbi
@sbi: Still understandable. :/
GMan
@GMan: Try a `boost::bind()` expression then where you are just missing one placeholder - still understandable?
Georg Fritzsche
@gf: Yes, but now you're cheating. :P
GMan
I'd say the *main* disadvantage with templates is poor error messages. They're not *easily* understandable, which is part of the reason concepts were going to be added to C++0x: to improve diagnostics. I'd say I understand templates very well, but even a mistake as simple as having a protected destructor on a class stored in a `unique_ptr` left me pondering a page-long error message for some time. Not helpful. (Yes, I'm stupid for doing that :P but people fat-finger things all the time, which is why good diagnostics are important)
AshleysBrain
+1 for number 6
Tim
+8  A: 

The only real disadvantage is that if you make any tiny syntax error in a template (especially one used by other templates) the error messages are not gonna be helpful... expect a couple pages of almost-unusable error msgs;-). Compilers' defect are very compiler-specific, and the syntax, while ugly, is not really "complex". All in all, though -- despite the huge issue with proper error diagnostics -- templates are still the single best thing about C++, the one thing that might well tempt you to use C++ over other languages with inferior implementations of generics, such as Java...

Alex Martelli
+1 Very true! Was hoping concepts in C++0x would've helped diagnostics.
AshleysBrain
+3  A: 

They're complicated for the compiler to parse which means your compilation time will increase. Also it can be hard to parse compiler error messages if you have advanced template constructions.

Andreas Brinck
+6  A: 

Templates expose your implementation to the clients of your code, which makes maintaining your ABI harder if you pass templated objects at library boundaries.

Alex B
+11  A: 

They are hard to validate. Template code which doesn't get used tends to be seldom compiled at all. Therefore good coverage of test cases is a must. But testing is time-consuming, and then it may turn out the code never needed to be robust in the first place.

Potatoswatter
That's an interesting one I hadn't considered before.
j_random_hacker
Another problem may be that templates can be used to *prevent* code from compiling: for example, ensuring that a class template is never instantiated with certain template parameters. It's generally hard to write tests verifying that code does not compile. :)
jalf
This is a good one, it's hard to make sure that all template methods work properly with a variety of data types when you may not need the full interface initially.
Mark B
+1  A: 

When you use templates, your compiler only generates what you actually use. I don't think there is any disadvantages in using C++ template meta-programming except the compiling time which can be quite long if you used very complex structures as boost or loki libraries do.

Opera
+6  A: 

So far no-one seems to have mentioned the main disadvantage I find with templates: code readability plummets!

I'm not referring to syntax issues -- yes the syntax is ugly, but I can forgive that. What I mean is this: I find that with never-seen-before non-templated code, however large the application is, if I start at main() I can usually decode the broad strokes of what a program is doing without problems. And code that merely uses vector<int> or similar doesn't bother me in the slightest. But once code starts to define and use its own templates for purposes beyond simple container types, understandability rapidly goes out the window. And that has very negative implications for code maintenance.

Part of that is unavoidable: templates afford greater expressiveness via the complicated partial-order overload resolution rules (for function templates) and, to a lesser degree, partial specialisation (for class templates). But the rules are so damn complicated that even compiler writers (who I'm happy to acknowledge as being an order of magnitude smarter than I am) are still getting them wrong in corner cases.

The interaction of namespaces, friends, inheritance, overloading, automatic conversions and argument-dependent lookup in C++ is already complicated enough. But when you add templates into the mix, as well as the slight changes to rules for name lookup and automatic conversions that they come with, the complexity can reach proportions that, I would argue, no human can deal with. I just don't trust myself to read and understand code that makes use of all these constructs.


An unrelated difficulty with templates is that debuggers still have difficulty showing the contents of STL containers naturally (as compared to, say, C-style arrays).

j_random_hacker
Template meta-programming can be a maintenance problem, sure. But more standard usage of templates is something all frequent C++ coders need to be able to grasp.I've had the opposite experience with debugging STL containers, at least with Visual Studio. std::vector, for example, is shown in the watch window as a collapsible entry that expands to the correct size of the container. With C-style arrays, you have to use the uglier "myArray,n" syntax, manually specifying the size of the array. The difference is even more dramatic with std::list or std::map vs. hand-written counterparts.
Ben
Thanks Ben, yes it's TMP that causes the most headaches. And that's good news about Visual Studio, I must have been summoning frustration caused by an earlier version. Or is that a feature of non-Express editions only?
j_random_hacker
+1  A: 

A disadvantage: template errors are only detected by the compiler when the template is instantiated. Sometimes, errors in the methods of templates are only detected when the member method is instantiated, regardless if the rest of the template is instantiated.

If I have an error in a method, of a template class, that only one function references, but other code uses the template without that method, the compiler will not generate an error until the erroneous method is instantiated.

Thomas Matthews
+1, but it would be more accurate to say that there are *some* errors that are not detected until the template is instantiated. Syntax errors, and type errors involving only non-dependent types, will be found at definition time.
j_random_hacker
+1  A: 

The absolute worst: The compiler error messages you get from bad template code.

Earlz
+2  A: 

Less people understand them, epsecially at the level of meta programming, therefore less people can maintain them.

Michael Dorgan