Abstract
Templates can be considered a generalization of ordinary classes and functions. Often a preexisting function or class, which is already tested, is promoted to a template, because of new software requirements; this will often save debugging time.
This is a preview of subscription content, log in via an institution.
Buying options
Tax calculation will be finalised at checkout
Purchases are for personal use only
Learn about institutional subscriptionsNotes
- 1.
A similar debate was raised about STL allocators. The notion of “equality of two containers of the same kind” obviously requires the element sequences to be equal, but it’s unclear whether this is also sufficient.
- 2.
While encapsulation conveys to the user a “sense of overhead,” interface adaptation suggests that the new sq is much better and can be used freely.
- 3.
In other words, callers of the code do not have to worry about inheritance. They can pass any T and the class will wrap it silently and automatically. This idea was developed further in a talk by Sean Parent and is freely downloadable from this link: http://channel9.msdn.com/Events/GoingNative/2013/Inheritance-Is-The-Base-Class-of-Evil .
- 4.
A non-mathematically inclined reader may want to consider an analogous case: software that executes a series of actions and at the same time records a list of “undo” steps.
- 5.
The interface of a data structure is frequently remodeled for ease of algorithms. This lesson was one of the milestones of the STL design.
- 6.
As a rule, hollow containers own no memory. You could object that here you could use a single scalar_t data member and return a reference to the same object for any pair of indices, but this strategy would consume a lot of CPU runtime, overwriting the same memory location for no purpose.
- 7.
Mind the use of token concatenation ##. You might be tempted to write operator ## OP to join operator and +, but this is illegal, because in C++, operator and + are two different tokens. On the other hand, ## is required between + and = to generate operator +=, so you need to write operator OP ## =.
- 8.
The user-defined constructor that converts T to ghost<T> is considered only after template argument deduction. Note that the constructor here is not even explicit. See [2], Section B.2.
- 9.
Hint: always leave a breakpoint in the conversion operator.
Author information
Authors and Affiliations
Rights and permissions
Copyright information
© 2015 Davide Di Gennaro
About this chapter
Cite this chapter
Gennaro, D.D. (2015). Refactoring. In: Advanced Metaprogramming in Classic C++. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-1010-9_10
Download citation
DOI: https://doi.org/10.1007/978-1-4842-1010-9_10
Publisher Name: Apress, Berkeley, CA
Print ISBN: 978-1-4842-1011-6
Online ISBN: 978-1-4842-1010-9
eBook Packages: Professional and Applied ComputingProfessional and Applied Computing (R0)Apress Access Books