Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawbacks to templates and the STL in C++ [closed]

Tags:

c++

templates

stl

Are there any drawbacks to using the STL or templates. Are there any situations for which they are inappropriate.

like image 944
minty Avatar asked Nov 05 '08 20:11

minty


People also ask

What are the disadvantage of using templates?

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.

What is the main problem with templates?

There are two main reasons why people dislike templates: They're confusing. They can lead to long compilation times.

What are disadvantages of using templates in C++?

First, many compilers historically have very poor support for templates, so the use of templates can make code somewhat less portable. Second, almost all compilers produce confusing, unhelpful error messages when errors are detected in template code. This can make templates difficult to develop.

What is the advantage of template functions?

Some of the advantages of using templates are: Templates simplify the creation of documents. Templates can ease our workload and make us feel less stressed, and, at the same time, they increase efficiency. Templates increase the attention of the audience.


3 Answers

First, you should use probably use them if they help you solve your problem. Templates are a very important part of C++ and have been part of the standard for years. STL is very powerful and fast at run time and should be supported on all decent compilers, but of course there are issues.

  • If you have a really old compiler, STL might not be completely supported.
  • The thread-safety of the STL implementation might be work for your application
  • Templates can lead to slower compile-times and possibly larger executable, especially with older compilers.
  • Compilers often produce incomprehensible error messages on code using templates.

just to name a few, but the drawbacks of not using them are likely to be much greater.

like image 196
David Nehme Avatar answered Nov 05 '22 22:11

David Nehme


Obvious disadvantages:

  • The syntax can be horrible - some bits of template syntax in C++ are really pushing the limits of sanity, and overlap with other parts of the language (e.g. >>)

  • Lots of people don't understand the STL very well, so you might restrict your audience.

  • Error messages tend to be hideously complicated.

  • The design of the STL collections tends to lead to a lot of copying of objects. The original 'smart pointer' (std::auto_ptr) wasn't suitable for use in most collections. Things have improved in this regard recently (TR1)

like image 40
Will Dean Avatar answered Nov 06 '22 00:11

Will Dean


There are several potential benefits and drawbacks

  • Templates expand the size of the resulting code.
  • Templated code is expanded and processed at compile time which may make compile times longer. (On the other hand, executable code may be more efficient).
  • Mis-used STL elements can result in slower code
  • The STL actually makes code more readable (my opinion differs from Will's). Like any language or library, you have to understand it to use it appropriately... which is a drawback for people who don't know it.
  • If you are using templates in a meta-programming sense (don't confuse w/ using the STL), the code looks like a language completely different from C++. It can be harder to parse through what the code is actually doing. (OTOH, done right - meta-programming makes you focus on architecture and design; it also brings more of the errors to compile time vs. runtime. This is a big win when you have a critical feature and you can catch an incorrectly coded piece at compile time rather than having a customer catch it during operation!)

Having said that, we use C++ and templates (and in some areas, meta-programming techniques) to the benefit of our overall code base. The code is slightly larger than it might be without templates, but the trade-offs in performance and maintainability outweigh the size. We do have skilled/experienced C++ programmers working on developing and maintaining the code.

If you're using drawbacks to decide whether to use C++ features/libraries or not - make sure you equally weigh the benefits both for the language and what your project/product/company is willing to trade off. Hope this helps.

Edit: One other major drawback I forgot to mention - portability. If you need to write portable code, templates may not be the right way to go. Most popular compilers today support the STL, however most is not all. The meta-programming techniques can be real killers to portability, so that is a definite consideration for deciding appropriateness of its use.

like image 8
twokats Avatar answered Nov 06 '22 00:11

twokats