What are the disadvantages of using templates?

19,590

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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).

Solution 5

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...

Share:
19,590
anish
Author by

anish

Updated on June 06, 2022

Comments

  • anish
    anish almost 2 years

    Some of the disadvantages would be

    1. its syntax is complex
    2. compiler generates extra code
  • GManNickG
    GManNickG about 14 years
    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.
  • Jeremy Friesner
    Jeremy Friesner about 14 years
    Well, he asked for any disadvantages, not only insurmountable disadvantages :)
  • sbi
    sbi about 14 years
    @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
    sbi about 14 years
    Anyway, +1 from me, even though I would start this list with #2, since anish's #2 is bogus.
  • GManNickG
    GManNickG about 14 years
    @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.
  • sbi
    sbi about 14 years
    @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>>(_Ra‌​nIt,_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.
  • Ankit Roy
    Ankit Roy about 14 years
    That's an interesting one I hadn't considered before.
  • GManNickG
    GManNickG about 14 years
    @sbi: Still understandable. :/
  • Georg Fritzsche
    Georg Fritzsche about 14 years
    @GMan: Try a boost::bind() expression then where you are just missing one placeholder - still understandable?
  • GManNickG
    GManNickG about 14 years
    @gf: Yes, but now you're cheating. :P
  • AshleysBrain
    AshleysBrain about 14 years
    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
    AshleysBrain about 14 years
    +1 Very true! Was hoping concepts in C++0x would've helped diagnostics.
  • josesuero
    josesuero about 14 years
    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. :)
  • Mark B
    Mark B about 14 years
    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.
  • Ankit Roy
    Ankit Roy about 14 years
    +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.
  • Ben
    Ben about 14 years
    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.
  • Ankit Roy
    Ankit Roy about 14 years
    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?