Making templates more flexible and extensible
The addition of templates in C++ gives us the ability to make certain types of classes and functions generically specified a single time by the programmer, while behind the scenes, the preprocessor generates many versions of that code on our behalf. However, in order for a class to truly be extensible to expand for many different user defined types, code written within member functions must be universally applicable to any type of data. To help with this endeavor, operator overloading can be used to extend operations that may easily exist for standard types to include definitions for user defined types.
To recap, we know operator overloading can allow simple operators to work not only with standard types but also with user defined types. By overloading operators in our template code, we can ensure that our template code is highly reusable and extensible.
Let’s consider how we can strengthen templates with the use of operator...