Templates are an excellent way to save time and avoid code duplication. Without having to enter 20 similar classes, each containing 15 member functions, you can enter a class template (class template) and have the compiler instantiate the 20 specific classes you need (specific class ) and 300 functions. (member functions of class template) are implicitly instantiated only when they are used, so you will get all 300 member functions (members functions) only if each function is actually in use. Function templates also have similar charms. Without having to write a lot of functions, you can write a function template and let the compiler do the rest. Isn't this a very important technique?
Yes, good. Sometimes. If you're not careful, using the templates (template) can lead to code bloat: repetitive (or almost repetitive) code, data, or binary code for both. The results will make the source code look compact and tidy, but the target code is bloated and loose. Being bloated and loose is rarely a fad, so you need to know how to avoid such binary expansions.
Your main tool has an imposing name commonality and variability analysis (versatility and variability), but there is nothing imposing on this idea. Even if you have never used a template in your career, you should do so from the beginning to the end.
When you write a function, and you realize that some part of the implementation of this function is essentially the same as the implementation of another function, would you just copy the code? Of course not. You separate the common code from the two functions, put it into a third function, and let the two functions call the new function. In other words, you analyze those two functions to find the common and changing artifacts, you move the generic artifacts into a new function and keep the changed artifacts in the original function. Similarly, if you write a class and you realize that some of the components of this class are the same as the artifacts of another class, you do not copy those generic artifacts. Instead, you move the generic widget into a new class, and then you use Inheritance (inheritance) or composition (compound) to make the original classes access to these common features. The different artifacts in the original classes--the changing artifacts--remain in their original positions.