Introduction

Get a brief introduction to templates in this lesson.

We'll cover the following

Templates

Templates are the feature that allows us to describe the code as a pattern for the compiler to generate program code automatically. Parts of the source code may be left to the compiler to be filled in until that part is actually used in the program.

Templates are very useful, especially in libraries, because they enable writing generic algorithms and data structures, instead of tying them to specific types.

Compared to the template support in other languages, D’s templates are very powerful and extensive. We will not get into all of the details of templates in this chapter. We will only cover function, struct, and class templates and type template parameters. You will see more about templates in the more templates chapter.

To see the benefits of templates let’s start with a function that prints values in parentheses:

Get hands-on with 1000+ tech skills courses.