# D Programming - Templates

Advertisements

Templates are the foundation of generic programming, which involve writing code in a way that is independent of any particular type.

A template is a blueprint or formula for creating a generic class or a function.

Templates are the feature that allows describing 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. The compiler fills in the missing parts.

## Function Template

Defining a function as a template is leaving one or more of the types that it uses as unspecified, to be deduced later by the compiler. The types that are being left unspecified are defined within the template parameter list, which comes between the name of the function and the function parameter list. For that reason, function templates have two parameter lists −

• template parameter list
• function parameter list
```import std.stdio;

void print(T)(T value) {
writefln("%s", value);
}

void main() {
print(42);

print(1.2);

print("test");
}
```

If we compile and run above code, this would produce the following result −

```42
1.2
test
```

## Function Template with Multiple Type Parameters

There can be multiple parameter types. They are shown in the following example.

```import std.stdio;

void print(T1, T2)(T1 value1, T2 value2) {
writefln(" %s %s", value1, value2);
}

void main() {
print(42, "Test");

print(1.2, 33);
}
```

If we compile and run above code, this would produce the following result −

``` 42 Test
1.2 33
```

## Class Templates

Just as we can define function templates, we can also define class templates. The following example defines class Stack and implements generic methods to push and pop the elements from the stack.

```import std.stdio;
import std.string;

class Stack(T) {
private:
T[] elements;
public:
void push(T element) {
elements ~= element;
}
void pop() {
--elements.length;
}
T top() const @property {
return elements[\$ - 1];
}
size_t length() const @property {
return elements.length;
}
}

void main() {
auto stack = new Stack!string;

stack.push("Test1");
stack.push("Test2");

writeln(stack.top);
writeln(stack.length);

stack.pop;
writeln(stack.top);
writeln(stack.length);
}
```

When the above code is compiled and executed, it produces the following result −

```Test2
2
Test1
1
```
Advertisements
{"ft_lang_label":"__label__en","ft_lang_prob":0.68496674,"math_prob":0.9054747,"size":2157,"snap":"2021-04-2021-17","text_gpt3_token_len":504,"char_repetition_ratio":0.12354854,"word_repetition_ratio":0.07017544,"special_character_ratio":0.2568382,"punctuation_ratio":0.16666667,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9809968,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-04-16T10:21:36Z\",\"WARC-Record-ID\":\"<urn:uuid:3d132102-2007-4607-9a5b-511f52e7943d>\",\"Content-Length\":\"29458\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c2ddef2c-861f-4aaa-8044-a125d8949e40>\",\"WARC-Concurrent-To\":\"<urn:uuid:b5be1cf6-ace4-4b15-a892-ca229c0c31b7>\",\"WARC-IP-Address\":\"72.21.91.42\",\"WARC-Target-URI\":\"http://www.tutorialspoint.com/d_programming/d_programming_templates.htm\",\"WARC-Payload-Digest\":\"sha1:M3I7A7UU4CLJ2F45BZYQ6ICTIVO5CUV4\",\"WARC-Block-Digest\":\"sha1:ZRUJ6XX6ZC6E5RK3TW2YI53TEKSBRI2T\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-17/CC-MAIN-2021-17_segments_1618038056325.1_warc_CC-MAIN-20210416100222-20210416130222-00417.warc.gz\"}"}