Generic programming: Difference between revisions

Content deleted Content added
m unpiped links using script
m Disambiguating links to Signature (disambiguation) (link changed to Type signature) using DisamAssist.
Line 88:
List<Car> list_of_cars;
</syntaxhighlight>
Above, <code>T</code> is a placeholder for whatever type is specified when the list is created. These "containers-of-type-T", commonly called [[template (programming)|templates]], allow a class to be reused with different datatypes as long as certain contracts such as [[Subtyping|subtype]]s and [[Type signature (computer science)|signature]] are kept. This genericity mechanism should not be confused with ''[[polymorphism (computer science)|inclusion polymorphism]]'', which is the [[algorithm]]ic usage of exchangeable sub-classes: for instance, a list of objects of type <code>Moving_Object</code> containing objects of type <code>Animal</code> and <code>Car</code>. Templates can also be used for type-independent functions as in the <code>Swap</code> example below:
 
<syntaxhighlight lang="Cpp">