Templates (6) Flashcards

1
Q

What is a template?

A

A template permits the definition of a template function or template class to use multiple data types

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

What is a function template?

A

A function template defines a specific behavior that can be applied to any data type
- Method overloading is used to provide function for each data type

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

What does the definition of a function template (code) look like?

A
template 
void function(T param){}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between a function template and a template function?

A

Function template: entire range of related functions with single code segment
Template functions: individually compiled versions of the function template

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

What is a class template?

A

Allows the program to contain a generic class definition that can be instantiated into a type specific object

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

What does the definition of a class template (code) look like?

A
.h file:
template
class ClassName{
public:
private:
}

ClassName.cpp:
template
ClassName::ClassName(){}

main.cpp
ClassName objectName();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between a function template and a template function?

A
Class templates: entire range of related classes with a single class definition
Template classes: individual instantiated versions of the class template
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a specialization of a template?

A

Providing a template with a set of template arguments
Not instantiation –> can cause instantiation
Non specialized version still needed

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

What does specialization of a template look like (code)?

A

template<>
int Stack::sum(){}
template
T Stack::sum() {}

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

Why can class template errors slip by the compiler?

A

The compiler does not evaluate class templates. The compiler needs to know the data types in order to evaluate the methods –> methods that are unused are not evaluated

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