12

Possible Duplicate:
What are the disadvantages of using templates?

Reading about templates I found out that for example if you not use a function from a class template, it will not generate code for that(a positive thing). I also saw that you can use compile time porgramming using templates and implement, let's say a factorial example and the result will be know at compile time.
So my question is: what are the negative aspects of using templates ?

Thank you.

Community
  • 1
  • 1
Adrian
  • 19,440
  • 34
  • 112
  • 219
  • 4
    a) The syntax. b) Manic overuse by some programmers. c) Voluminous compiler error messages when things go wrong. –  May 06 '11 at 11:23
  • @liak: it took a while to find the duplicate :)) so whats its my foult that it didint appear when i worte the title ? – Adrian May 06 '11 at 11:41
  • It ain't your fault. I just read it before and that's it... I too agree it is a valid question (but an answer already available though) and I dint downvote either.. :) – liaK May 06 '11 at 11:47
  • @liak: doesnt matter the votes, i was just curios about this answer, and I know I can get good answer here :) – Adrian May 06 '11 at 11:51
  • "I found out that for example if you not use a function from a class template, it will not generate code for that" <-- the same is true for plain old code. Compilers nowadays are smart. In fact, from my experience, compilers actually do better when optimizing plain old codes (reasonable). – kizzx2 May 06 '11 at 12:02

7 Answers7

12

Compile time. Complex, especially recursive templates can take ages to compile.

Error messages. Template error messages are terrifying and generally not very helpful. Concepts would've been great, but sadly the language committee has dropped them from the upcoming standard.

Readability. Templates code can be challenging to read.

Difficulty Much of the underlying tricks rely on not-so-well-known aspects of the language standard, so one needs a decent knowledge of the language to get along with them.

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
  • 5
    On the upside, they can make the programmer who wrote them feel really smart :) Also, CLang is a compiler that tries to generate more readable template error messages. – Björn Pollex May 06 '11 at 11:28
  • 2
    about compile time, hence due to Templates, the compilation of CPP is equivalant to a turing machine, the compilation might even not end at all. – amit May 06 '11 at 11:31
  • I digress on the "generally not very helpful" bit. Though especially in the case of STL (which, despite its ingenuity, is an insane abuse of the language) you sometimes have to read through about 10 lines of error message, the message nevertheless contains the exact problem (usually in the last 15-20 characters). For your own not-insane templates, error messages are usually no more than 1-2 lines and in the regular case very concisely tell you what's wrong. – Damon May 06 '11 at 11:36
  • 1
    @amit: ISO 14882 14.7.1 (14) states that there is an implementation-dependent limit on template recursion, infinite recursion is undefined. Thus, compilation must necessarily end (successfully or unsuccessfully). – Damon May 06 '11 at 11:44
9

In order for clients to use templates you developed, you have to deliver the source code. This is probably the most significant downside I have encountered in practice.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • 1
    I'd add that the worst part of that is that they can't be exported from shared library, so when you fix a bug, the code using them has to be recompiled to benefit from it. In contrast many changes in non-template code allow just replacing the shared library. That's especially important when fixing security issues. – Jan Hudec May 06 '11 at 11:35
3
  • longer compile time
  • compiler error messages that are harder to read
Frank Schmitt
  • 30,195
  • 12
  • 73
  • 107
3

You can't make template member functions within classes virtual. That is:

class Foo{
public:
    template<typename T>
    void doSomething(const T& aThing);

    virtual void somethingElse();
//rest of class
};

class Bar : public Foo{
public:
    template<typename T>
    void doSomething(const T& aThing);

    virtual void somethingElse();
};

Foo* var = new Bar();
var->doSomething(1); //will call Foo's method, not Bar.
var->somethingElse(); //will call Bar's method.  It's polymorphic.

and this often causes problems for programmers.

wheaties
  • 35,646
  • 15
  • 94
  • 131
  • The virtual `somethingElse` method is just confusing there while you didn't mark the one that can't be made virtual (it indeed can't be). – Jan Hudec May 06 '11 at 11:29
  • Can you provide any resources to confirm? – atoMerz May 06 '11 at 11:29
  • @Jan Hudec Ok, filled it out. Sorry to not confuse. @AtoMerz I don't need to provide any resources. The template method is not `virtual` which means it won't behave polymorphically. Just try to compile one on a standards compliant compiler. It'll complain. – wheaties May 06 '11 at 11:32
  • I guess his right because templates are static no ? any function template would resolve to a static call – Adrian May 06 '11 at 11:34
  • 1
    @vBx: No, templates don't have to be static. The problem is that a method template is any number of methods, one for each instantiation. Since the compiler does not know what instantiations there will be when compiling the class, it can't allocate entries in the virtual method table for them and therefore they can't be virtual (remember, that different instantiations may be used in different compilation units, but the virtual methods table layout must be the same for all of them). – Jan Hudec May 06 '11 at 12:11
2
  1. Syntax is intimidating.
  2. Not easy to debug the code generated from templates.
  3. You have to provide all your code in headers. No intellectual property rights.
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Most debuggers work with template code just fine. They are a bit harder to debug because as higher level concept they are harder to understand, but definitely far from impossible to debug. – Jan Hudec May 06 '11 at 11:26
  • Exactly! harder but not impossible. – atoMerz May 06 '11 at 11:27
  • @AtoMerZ: Jan's answer ssays it out – Alok Save May 06 '11 at 11:28
  • @AtoMerZ: Seems you posted while I was editing, I did say difficult not impossible. – Alok Save May 06 '11 at 11:29
  • What makes you think you don't have intellectual property rights to your header files? – Christopher Creutzig May 06 '11 at 11:35
  • @Christopher Creutzig: Providing code as binaries reduces the chances of the code getting poached rather than providing templates which essentially would reveal the code. – Alok Save May 06 '11 at 12:03
  • There are minimal problems with the common cases, but metaprograms (the stuff that's supposed to be calculated at compile-time) can't be debugged. – Jan Hudec May 06 '11 at 12:16
  • @Als: Obviously. Shipping compiled code makes violating your IP more difficult for your customers. But it does not change your rights, and if your customers don't understand that, you're in the bad position of deciding if you want to sue them. But you can get there with binaries, too. (Also, I've seen template headers just providing a thin typesafe wrapper around non-typesafe code shipped as binaries and not meant to be used directly.) – Christopher Creutzig May 06 '11 at 12:57
1

They may increase the size of your executable exponentially with the template nesting depth.

They may increase the compile time exponentially.

Compiler messages can be hard to understand.

Gunther Piez
  • 29,760
  • 6
  • 71
  • 103
1
  1. It's generally impossible to decipher your types in the watch window of a debugger because they are around a thousand characters long. So you hit a break point in the debugger, you open your watch window to inspect your map, and in the 'type' column, you are spammed with a mess of text that is completely indecipherable.

  2. Possibility of overkill. That is using a template for everything! But that is left to the programmer to determine. I"m just saying that I've seen programmers who use it for everything.

C.J.
  • 15,637
  • 9
  • 61
  • 77