In C++, templates are a feature that supports generic programming (code that is more type-independent). The template prefix is given by template <class T>
(where is a type parameter that can change).
Templates generally increase compilation time (though it’s questionable how significant this can get). The compiler generally builds a new type-specific version every time it’s called.
C++ doesn’t allow separation of interface and implementation for template definitions so they must be included with the code that uses it. Some approaches: 1) only have a header file (or alternatively a .hpp
file) and no implementation file or 2) #include
the implementation file (.cpp
) for the template class instead of the header file or 3) have two header files and #include
one in the other.
The C++ Standard Template Library includes several templated classes implemented with methods for us. They vastly expand the language’s functionality.