Exploring Type Erasure

In this chapter, you will learn what type erasure (also known as type erasing) is as well as how to use it in your own applications. This chapter is important because type erasing provides the ability to work with objects of different types without the need for the object to share a common base class. 

This chapter starts with a simple explanation of type erasure by explaining how type erasure works in the C language as well as how to perform type erasure in C++ using inheritance. The next recipe will provide a different approach to type erasure using C++ templates, which will teach you how C++ concepts are used to define a type's specifications instead of the type itself.

Next, we will walk through the classic C++ type erasure pattern. This recipe will teach you the skills to erase type information, providing the ability to create type-safe, generic code. Finally, we will conclude with a comprehensive example of using type erasure to implement the delegate pattern, which is a pattern that provides the ability to wrap callable objects of any type and is used heavily by languages such as ObjC.

The recipes in this chapter are as follows:

  • How to erase a type with inheritance
  • Using C++ templates to write generic functions
  • Learning the C++ type eraser pattern
  • Implementing the delegate pattern
..................Content has been hidden....................

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