What are the disadvantages of using templates?

前端 未结 11 611
生来不讨喜
生来不讨喜 2020-12-16 01:02

Some of the disadvantages would be

  1. its syntax is complex
  2. compiler generates extra code
相关标签:
11条回答
  • 2020-12-16 01:27

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

    0 讨论(0)
  • 2020-12-16 01:35

    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.

    0 讨论(0)
  • 2020-12-16 01:36

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

    0 讨论(0)
  • 2020-12-16 01:37

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

    0 讨论(0)
  • 2020-12-16 01:39

    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

    0 讨论(0)
  • 2020-12-16 01:43

    I have used templates sometimes over the years. They can be handy but from a professional perspective, I am leaning away from them. Two of the reasons are:

    1.

    The need to either a.) expose the function definitions (not only declarations) "source" code to the "where used" code or b.) create a dummy instantiation in the source file. This is needed for compilation. Option a.) can be done by defining functions in the header or actually including the cpp.

    One of the reasons that we tolerate headers in C++ (compared to C# for example) is because of the separation of "interface" from "implementation". Well, templates seem to be inconsistent with this philosophy.

    2.

    Functions called by a template type parameter instantiation may not be enforced at compile time resulting in link errors. E.g. T example; example.CompilerDoesntKnowIfThisFunctionExistsOnT(); This is "loose" IMHO.

    Solutions:

    Rather then templates, I lean towards using a base class whereby the derived/container classes know what is available at compile time. The base classes can provide the generic methods and "types" that templates are often used for. This is why source code availability can be helpful if existing code needs to be modified to insert a generic base class in the inheritance hierarchy where needed. Otherwise if, code is closed source, rewrite it better using generic base classes instead of using a template as a work around.

    If type is unimportant e.g. vector< T > then how about just using"object". C++ has not provided an "object" keyword and I have proposed to Dr. Bjarne Stroustrup that this would be helpful especially to tell compiler and people reading code that type is not important (for cases when it isn't). I don't that think C++11 has this, perhaps C++14 will?

    0 讨论(0)
提交回复
热议问题