Generic programming: Difference between revisions

Content deleted Content added
Advantages and disadvantages: Small WP:COPYEDITs WP:EoS: WP:TERSE, clarify. MOS:FIRSTABBReviation clarify, define before parenthetic WP:ABBR.
Line 239:
 
=====Advantages and disadvantages=====
Some uses of templates, such as the <code>max()</code> function, were previouslyformerly filled by function-like [[preprocessor]] [[Macro (computer science)|macros]] (a legacy of the [[C (programming language)|C]] language). For example, here is a possible implementation of such macro:
 
<syntaxhighlight lang="cpp">
Line 245:
</syntaxhighlight>
 
Macros are expanded (copy pasted) by the [[preprocessor]], before compilingprogram propercompiling; templates are actual real functions. Macros are always expanded inline; templates can also be [[inline function]]s when the compiler deems it appropriate.
 
However, templates are generally considered an improvement over macros for these purposes. Templates are type-safe. Templates avoid some of the common errors found in code that makes heavy use of function-like macros, such as evaluating parameters with side effects twice. Perhaps most importantly, templates were designed to be applicable to much larger problems than macros.
 
There are four primary drawbacks to the use of templates: supported features, compiler support, poor error messages (usually with pre C++20 ''substitution failure is not an error'' ([[SFINAE]])), and [[code bloat]]:
# Templates in C++ lack many features, which makes implementing them and using them in a straightforward way often impossible. Instead programmers have to rely on complicatedcomplex tricks which leads to bloated, hard to understand and hard to maintain code. Current developments in the C++ standards exacerbate this problem by making heavy use of these tricks and building a lot of new features for templates on them or with them in mindintended.
# Many compilers historically had poor support for templates, thus the use of templates could have mademake code somewhat less portable. Support may also be poor when a C++ compiler is being used with a [[Linker (computing)|linker]] that is not C++-aware, or when attempting to use templates across [[Library (computer science)#Shared libraries|shared library]] boundaries.
# Compilers can produce confusing, long, and sometimes unhelpful error messages when errors are detected in code that uses SFINAE.<ref>[https://www.stroustrup.com/N1522-concept-criteria.pdf Stroustrup, Dos Reis (2003): Concepts - Design choices for template argument checking]</ref> This can make templates difficult to develop with.
# Finally, the use of templates requires the compiler to generate a separate ''instance'' of the templated class or function for every type parameters used with it. (This is necessary because types in C++ are not all the same size, and the sizes of data fields are important to how classes work.) So the indiscriminate use of templates can lead to [[code bloat]], resulting in excessively large executables. However, judicious use of template specialization and derivation can dramatically reduce such code bloat in some cases:{{Blockquote|So, can derivation be used to reduce the problem of code replicated because templates are used? This would involve deriving a template from an ordinary class. This technique proved successful in curbing code bloat in real use. People who do not use a technique like this have found that replicated code can cost megabytes of code space even in moderate size programs.|[[Bjarne Stroustrup]]|The Design and Evolution of C++, 1994<ref name="Stroustrup94Design">{{cite book |lastlast1=Stroustrup |firstfirst1=Bjarne |authorauthor1-link=Bjarne Stroustrup |year=1994 |title=The Design and Evolution of C++|year=1994 |publisher=Addison-Wesley |___location=Reading, Massachusetts |isbn=978-81-317-1608-3 |pages=346–348 |chapter=15.5 Avoiding Code Replication |bibcode=1994dec..book.....S}}</ref>}}
# Templated classes or functions may require an ''explicit specialization'' of the template class which would require rewriting of an entire class for a specific template parameters used by it.
The extra instantiations generated by templates can also cause some debuggers to have difficulty working gracefully with templates. For example, setting a debug breakpoint within a template from a source file may either miss setting the breakpoint in the actual instantiation desired or may set a breakpoint in every place the template is instantiated.