Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Generic programming
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=====Technical overview===== There are many kinds of templates, the most common being function templates and class templates. A ''function template'' is a pattern for creating ordinary functions based upon the parameterizing types supplied when instantiated. For example, the C++ Standard Template Library contains the function template <code>max(x, y)</code> that creates functions that return either ''x'' or ''y,'' whichever is larger. <code>max()</code> could be defined like this: <syntaxhighlight lang="cpp"> template<typename T> T max(T x, T y) { return x < y ? y : x; } </syntaxhighlight> ''Specializations'' of this function template, instantiations with specific types, can be called just like an ordinary function: <syntaxhighlight lang="cpp"> std::cout << max(3, 7); // Outputs 7. </syntaxhighlight> The compiler examines the arguments used to call <code>max</code> and determines that this is a call to <code>max(int, int)</code>. It then instantiates a version of the function where the parameterizing type <code>T</code> is <code>int</code>, making the equivalent of the following function: <syntaxhighlight lang="cpp"> int max(int x, int y) { return x < y ? y : x; } </syntaxhighlight> This works whether the arguments <code>x</code> and <code>y</code> are integers, strings, or any other type for which the expression <code>x < y</code> is sensible, or more specifically, for any type for which <code>operator<</code> is defined. Common inheritance is not needed for the set of types that can be used, and so it is very similar to [[Duck typing#Templates or generic types|duck typing]]. A program defining a custom data type can use [[operator overloading]] to define the meaning of <code><</code> for that type, thus allowing its use with the <code>max()</code> function template. While this may seem a minor benefit in this isolated example, in the context of a comprehensive library like the STL it allows the programmer to get extensive functionality for a new data type, just by defining a few operators for it. Merely defining <code><</code> allows a type to be used with the standard <code>sort()</code>, <code>stable_sort()</code>, and <code>binary_search()</code> algorithms or to be put inside data structures such as <code>set</code>s, [[heap (programming)|heap]]s, and [[associative array]]s. C++ templates are completely [[type safe]] at compile time. As a demonstration, the standard type <code>complex</code> does not define the <code><</code> operator, because there is no strict order on [[complex number]]s. Therefore, <code>max(x, y)</code> will fail with a compile error, if ''x'' and ''y'' are <code>complex</code> values. Likewise, other templates that rely on <code><</code> cannot be applied to <code>complex</code> data unless a comparison (in the form of a functor or function) is provided. E.g.: A <code>complex</code> cannot be used as key for a <code>map</code> unless a comparison is provided. Unfortunately, compilers historically generate somewhat esoteric, long, and unhelpful error messages for this sort of error. Ensuring that a certain object adheres to a [[protocol (computer science)|method protocol]] can alleviate this issue. Languages which use <code>compare</code> instead of <code><</code> can also use <code>complex</code> values as keys. Another kind of template, a ''class template,'' extends the same concept to classes. A class template specialization is a class. Class templates are often used to make generic containers. For example, the STL has a [[linked list]] container. To make a linked list of integers, one writes <code>list<int></code>. A list of strings is denoted <code>list<string></code>. A <code>list</code> has a set of standard functions associated with it, that work for any compatible parameterizing types.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)