...

/

The Shortcut Syntax

The Shortcut Syntax

The shortcut syntax of templates is explained in this lesson.

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
...