Skip to main content

Class Templates

  • Chapter
  • First Online:
Beginning C++17
  • 4843 Accesses

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.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

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

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2018 Ivor Horton and Peter Van Weert

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

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

Publish with us

Policies and ethics