Generic programming: Difference between revisions

Content deleted Content added
Tags: Mobile edit Mobile web edit
Tags: Mobile edit Mobile web edit
Line 244:
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 [[doubly linked list]] container, <code>std::list</code> (equivalent to <code>LinkedList</code>). To make a linked list of integers, one writes <code>std::list<int></code>. A list of strings is denoted <code>std::list<std::string></code>. A <code>std::list</code> has a set of standard functions associated with it, that work for any compatible parameterizing types.
 
[[C++20]] supportsintroduces constraining template types using [[Concepts (C++)|concepts]]. Constraining the <code>max()</code> could look something like this:
 
<syntaxhighlight lang="cpp">
Line 255:
 
// in requires clause:
 
template <typename T>
requires std::totally_ordered<T>
Line 262 ⟶ 261:
return x < y ? y : x;
}
 
</syntaxhighlight>