Skip to main content

Refactoring

  • Chapter
  • 2758 Accesses

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

Chapter
USD   29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD   69.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD   89.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Learn about institutional subscriptions

Notes

  1. 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. 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. 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. 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. 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. 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. 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. 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. 9.

    Hint: always leave a breakpoint in the conversion operator.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints 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

Publish with us

Policies and ethics