is (/* ... */ Specifier, TemplateParamList)

Learn the use of “is” expression with Specifier and TemplateParamList.

We'll cover the following

Different syntaxes of is

There are four different syntaxes of the is expression that use a template parameter list:

  • is (T : Specifier, TemplateParamList)

  • is (T == Specifier, TemplateParamList)

  • is (T identifier : Specifier, TemplateParamList)

  • is (T identifier == Specifier, TemplateParamList)

These syntaxes allow for more complex cases. Identifier, Specifier, :, and == all have the same meanings as described above.

TemplateParamList

TemplateParamList is both a part of the condition that needs to be satisfied and a facility to define additional aliases if the condition is indeed satisfied. It works in the same way as template type deduction.

As a simple example, let’s assume that an is expression needs to match associative arrays that have keys of type string:

static if (is (T == Value[Key], // (1)
               Value,           // (2)
               Key : string)) { // (3)

That condition can be explained in three parts where the last two are parts of the TemplateParamList:

  1. If T matches the syntax of Value[Key]
  2. If Value is a type
  3. If Key is string (remember template specialization syntax)

Having Value[Key] as the Specifier requires that T is an associative array. Leaving Value as is means that it can be any type. Additionally, the key type of the associative array must be string. As a result, the previous is expression means “if T is an associative array where the key type is string.”

The following program tests that is expression with four different value types:

Get hands-on with 1200+ tech skills courses.