Template Metaprogramming Flashcards

1
Q

What are template functions?

A

When we write the templates of functions that don’t necessarily explicitly specify the types of parameters/return types for function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does it mean that template functions are candidate functions?

A

When you attempt to use a function, the compiler will search for all templated functions and try to find a suitable candidate.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What happens if a compiler find a suitable templated function?

A

It will create an instance of that function for the specified types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is each instance of a template function in the binary?

A

Each instance of the templated function is a different function in the binary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Where do we write template functions?

A

Only in the header files. As they are not implementations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do constrain templates?

A

Using them in a way that defines their behaviour. E.g. using them in a to string function means type must support &laquo_space;operator

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why might template functions printing a class not work?

A

Because the print function of a class might not be defined.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens to function calls during linking in compilation?

A

During compilation, when the compiler comes across a function call it must resolve that function call to a specific instance of a function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

During linking what happens if there is a perfectly matching implemented function?

A

It will link the call to the function implementation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

During linking what happens if there is not a perfectly matching implemented function?

A
  • The compiler will look through all the template functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What happens in you include a template in an implementation file that wasn’t used?

A

The template would not be included in an object file. (Why it needs to be in a header file)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What happens during linking in template metaprogramming?

A

During compilation, if the compiler reaches a call to a function that has no implementation, rather than throw and error and abort compilation, it will try to find a candidate function to create a specialisation of it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the compiler process for finding candidate function?

A
  • Compiler creates a set of all functions with a matching name
  • Then tries to substitute in the type(s) from the call into the implementation
  • If one matches, the compiler will create an instance of the specialisation of the function for a given type
  • If one doesn’t match, it will move onto the next template in the set
  • An error only unfolds if there are no possible candidate template functions
    (Process also known as SFINAE)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does SFINAE stand for?

A

Substitution Failure Is Not An Error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is SFINAE?

A

The concept of metaprogramming where the compiler tries to find a candidate function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are template classes?

A

Classes that are defined using templates, allowing them to parameterised with one more types or non-type parameters.

17
Q

What can templated functions and classes be proposed to the compiler for?

A

Template functions and classes are provided to the compiler as blueprints and the compiler decides whether to include them in the final executable program based on whether they are actually used in the codebase.

18
Q

What does writing our own specialisations allows us to do?

A

Provide a different implementation for specific substitutes of the template parameters.

19
Q

What is specified in full specialisation?

A

We provide a type for all template parameters in the original template

20
Q

What is partial specialisation?

A

(Classes only) we provide types for some but not all template parameters.

21
Q

Why might template metaprogramming be bad for large projects?

A

As it reconciles the code each time, it can add a lot of time to computation.

22
Q

What can we do to work around the compiler going through all possible templates?

A

Specify a base type or class as a template parameter.

23
Q

What happens if templates are never called?

A

If templates are never called, nothing will be included in the binary

24
Q

Can template functions be partially specialized?

A

No

25
Q

What type of specialization can template functions be?

A

Only fully specialized.

26
Q

What type of specialization can template classes be?

A

Fully or partially specialized.

27
Q

What are template specializations?

A

Examples of template functions with certain types, similar to polymorphism how a function or class can have the same function with different parameters and arguments.

28
Q

What is full specialization?

A

All template parameters have been specified