template specialization Sometimes, the programmer may decide to implement a special version of a function (or class) for a given set of template type arguments which is called an explicit specialization. In this way certain template types can have a specialized implementation that is optimized for the type or a more meaningful implementation than the generic implementation. If a class template is specialized by a subset of its parameters it is called partial template specialization (function templates cannot be partially specialized). If all of the parameters are specialized it is a full specialization. Explicit specialization is used when the behavior of a function or class for particular choices of the template parameters must deviate from the generic behavior: that is, from the code generated by the main template, or templates. For example, the template definition below defines a specific implementation of max() for arguments of type const char*: #include <cstring> template<> const char* max(const char* a, const char* b) { // Normally, the result of a direct comparison // between two C strings is undefined behaviour; // using std::strcmp makes defined. return std::strcmp(a, b) > 0 ? a : b; } Variadic templates C++11 introduced variadic templates, which can take a variable number of arguments in a manner somewhat similar to variadic functions such as std::printf. Template aliases C++11 introduced template aliases, which act like parameterized typedefs. The following code shows the definition of a template alias StrMap. This allows, for example, StrMap<int> to be used as shorthand for std::unordered_map<int,std::string>. template<typename T> using StrMap = std::unordered_map<T, std::string>; Generic programming features in other languages Initially, the concept of templates was not included in some languages, such as Java and C# 1.0. Java's adoption of generics mimics the behavior of templates, but is technically different. C# added generics (parameterized types) in .NET 2.0. The generics in Ada predate C++ templates. Although C++ templates, Java generics, and .NET generics are often considered similar, generics only mimic the basic behavior of C++ templates. Some of the advanced template features utilized by libraries such as Boost and STLSoft, and implementations of the STL itself, for template metaprogramming (explicit or partial specialization, default template arguments, template non-type arguments, template template arguments, ...) are not available with generics. In C++ templates, compile-time cases were historically performed by pattern matching over the template arguments. For example, the template base class in the Factorial example below is implemented by