What are the advantages and disadvantages of using templates-00999
This subjective question is related to the book/course vu cs605 Software EngineeringII. It can also be found in vu cs605 Mid Term Solved Past Paper No. 2.
Question 1: What are the advantages and disadvantages of using templates?
Templates are type-safe. This is because the types that templates act upon are known at compile time, so the compiler can perform type checking before errors occur.
Templates can be easier to understand, since they can provide a straightforward way of abstracting type information.
It helps in utilizing compiler optimizations to the extreme. Then of course there is room for misuse of the templates. On one hand they provide an excellent mechanism to create specific type-safe classes from a generic definition with little overhead.
They can present seriously confusing syntactical problems esp. when the code is large and spread over several header and source files.
Then, there are times, when templates can "excellently" produce nearly meaningless compiler errors thus requiring extra care to enforce syntactical and other design constraints. A common mistake is the angle bracket problem.
Answer:
Many things can be possible without using templates but it does offer several clear advantages not offered by any other techniques:
Advantages:
Templates are easier to write than writing several versions of your similar code for different types. You create only one generic version of your class or function instead of manually creating specializations.Templates are type-safe. This is because the types that templates act upon are known at compile time, so the compiler can perform type checking before errors occur.
Templates can be easier to understand, since they can provide a straightforward way of abstracting type information.
It helps in utilizing compiler optimizations to the extreme. Then of course there is room for misuse of the templates. On one hand they provide an excellent mechanism to create specific type-safe classes from a generic definition with little overhead.
Disadvantages:
On the other hand, if misused
Templates can make code difficult to read and follow depending upon coding style.They can present seriously confusing syntactical problems esp. when the code is large and spread over several header and source files.
Then, there are times, when templates can "excellently" produce nearly meaningless compiler errors thus requiring extra care to enforce syntactical and other design constraints. A common mistake is the angle bracket problem.