So why do we use classes at all and not just make everything a template? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

So why do we use classes at all and not just make everything a template?

25th Jul 2016, 8:06 AM
ethan
ethan - avatar
3 Answers
+ 4
@ethan: There are differences that make certain things easier / more difficult. *Readability*: Normal functions are easier to read since they state more clearly what the do. *Potentially more bugs*: Normal functions are designed to fulfill a specific purpose using specific types. Templates have to take into consideration that types behave differently, e.g. integer type division may occur giving you different results for the same values... *Compiler error messages*: Making an error in a template leads to cryptic error messages from the most compilers nowadays. The new feature "concepts" which is to become part of the C++17 (aka. C++1z) standard is intended to solve this problem but we're not there yet (and we'll have to see, if they really help). *Optimization*: Specific functions or classes are potentially more optimized for the types they are using. There's template specialization to handle this, true, but template specialization adds to the complexity of the code again and therefore its readability. *Compile time*: Using many templates increases compile time in comparison to non-template elements (the Boost.MSM library is an example for a significant increase in compile time due to excessive use of templates). This increased compile time prolongs the write - compile - test cycles thar every iteratively working developer does. I hope I could outline some of the tradeoffs one has to make when using templates. If the negative consequences of using templates outweigh the gain templates are obviously not the best choice and that's why you should not make everything a template.
26th Jul 2016, 12:42 PM
Stefan
Stefan - avatar
0
templates are not instead of classes.
25th Jul 2016, 8:38 PM
Bernhard Eriksson
Bernhard Eriksson - avatar
0
@Bernhard Eriksson They seem to do the same thing, template classes and template functions just seem to be better variants of normal classes and normal functions
26th Jul 2016, 9:23 AM
ethan
ethan - avatar