Abstract
Class templates are a powerful mechanism for generating new class types automatically. A significant portion of the C++ Standard Library is built entirely on the ability to define templates. Both function and class templates are used extensively throughout the Library to provide versatile, generic utilities, algorithms, and data structures.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 1.
The actual swap<>() template is different in two aspects. First, it moves the objects if possible using move semantics. You’ll learn all about move semantics in the next chapter. Second, it is only conditionally noexcept. Concretely, it is noexcept if its arguments can be moved without exceptions. Conditional noexcept specifications are a more advanced language feature we do not cover in this book.
- 2.
The reason we cannot use the std::swap() from within our copy assignment operator is that std::swap() in turn would use the copy assignment operator. In other words, calling std::swap() here would result in infinite recursion!
Author information
Authors and Affiliations
Rights and permissions
Copyright information
© 2018 Ivor Horton and Peter Van Weert
About this chapter
Cite this chapter
Horton, I., Van Weert, P. (2018). Class Templates. In: Beginning C++17. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-3366-5_16
Download citation
DOI: https://doi.org/10.1007/978-1-4842-3366-5_16
Published:
Publisher Name: Apress, Berkeley, CA
Print ISBN: 978-1-4842-3365-8
Online ISBN: 978-1-4842-3366-5
eBook Packages: Professional and Applied ComputingProfessional and Applied Computing (R0)Apress Access Books