The Shortcut Syntax
Explore the shortcut syntax for defining templates in D, including function, struct, and class templates. Understand how this syntax maps to full template definitions and how eponymous templates work. Learn to create template namespaces and leverage type-based conditions for flexible and reusable code.
We'll cover the following...
You saw the power and convenience of templates in the templates chapter. A single templated definition of an algorithm or a data structure can effectively be used for multiple types.
The earlier chapter covered only the most common uses of templates, including function, struct, and class templates and their uses with type template parameters. In this chapter, we cover templates in more detail. Before going further, we recommend that you review the following templates summary.
The shortcut syntax
In addition to being powerful, D templates are easy to define and use, and they are very readable. Defining a function, struct, or class template is as simple as providing a template parameter list:
T twice(T)(T value) {
return 2 * value;
}
class Fraction(T) {
T numerator;
T ...