Motivation
Templates exist to enable generic programming, allowing you to write functions and classes that can operate with any data type without duplicating code for each type. They help reduce redundancy, improve maintainability, and allow the compiler to generate optimized code for each specific type automatically.
You create templates when you want to run specific logic on a handful of types, or potentially many types, while keeping a single, flexible implementation. This is especially useful for containers, algorithms, and utilities that need to work with different types in a type-safe manner.