...

/

Named Template Constraints

Named Template Constraints

We will explore the named template constraints in this lesson.

We'll cover the following...

Named template constraints

Sometimes, the constraints are complex, making it hard to understand the requirements of template parameters. This complexity can be handled by an idiom that effectively gives names to constraints. This idiom combines four features of D:

  • anonymous functions
  • typeof
  • is expression
  • eponymous templates

Let’s see this in a function template that has a type parameter. The template uses its function parameter in specific ways:

void use(T)(T object) { 
    // ...
    object.prepare();
    // ...
    object.fly(42);
    // ...
    object.land();
    // ...
}

As is obvious from the implementation of the template, the types that this function can work with ...