Using Templates for Generic Programming

In this chapter, we will learn advanced template programming techniques. These techniques include the ability to change the implementation of a template class based on the type that is provided, how to work different types of arguments including how to properly forward them, how to optimize your code both at runtime and compile time, and how to use some new features added to C++17. This is important because it provides a better understanding of how template programming works as well as how to ensure your templates are performing the way you expect.

Too often, we write template code assuming it is executing one way when, in fact, it is executing in another, either generating unreliable code, code with unexpected performance penalties, or both. This chapter will explain how to avoid these issues and provide the foundation for writing proper generic programs.

The recipes in this chapter are as follows:

  • Implementing SFINAE
  • Learning perfect forwarding
  • Using if constexpr
  • Using tuples to work with parameter packs
  • Using traits to vary the behavior of template implementations
  • Learning how to implement template<auto>
  • Working with explicit template declarations

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset