Templates Flashcards

1
Q

True or False: A template can be declared inside of any function.

A

False.

A template must be declared outside of other functions. It may be declared inside of other data structures like classes, however.

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

True or False: All compilers treat the contents of a template function as existent code that the compiler needs to verify.

A

False.

Short answer: Some do, some don’t.

Long answer: Some compilers ignore the body of a template function (even if it contains errors or undefined references) as long as the template function is never called anywhere. Essentially, that code doesn’t exist because it’s never used.

Other compilers actually do check for errors in the template body and fail to compile if any are detected -even if- the template function is never called anywhere.

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

What does the compiler do with a template function when it’s called somewhere in the code?

A

It generates a function overload for the data type(s) being used by the template function. This allows the programmer to write one function instead of an individual overload function for each and every data type that could be processed by that function.

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

What is the basic syntax of a Template declaration?

A

template <typename>
returnType FunctionName(T parameter);</typename>

template <class>
ClassName ();</class>

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

True or False: All operations within a template need to be valid for every potential data type that the template uses or instantiates.

A

True.

The template is just creating an overload for the data type, so if the code body can’t do anything legal with that data type, it’s not going to function correctly.

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

How are member functions written in a template CLASS?

A

As template functions.

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

How does typedef work?

A

It allows a type to be defined by a variable instead of its normal identifier.

For example, you could say:

typedef int mytype;

Now any time mytype is used, the compiler treats it as though it is an integer. If you wanted to change all instances of mytype to double, it would be as simple as changing the above line to:

typedef double mytype;

This would require editing and recompiling the code each time the type needed to change, however. It was a simple way to do this in C, but in C++, templates can eliminate the need for this and create more generic all-purpose code.

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

Can a template have multiple arguments?

A

Yes.

template <typename T, int size>

In the above declaration, there’s a generic typename placholder T, and then a specified int argument: size.

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

What is the syntax of defining a function of a template class?

A

template <class>
returnType ClassName<T>::FunctionName(int p)
{
// Function body code
}</T></class>

// You must use the whole template format for each
// definition along with the scope resolution operator

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

When templates, what needs to be included in any main program that uses them?

A

The entire definition of the template function(s).

This means that either the entire class needs to be defined in the header file that’s being included in the main file, or the .cpp definition file itself needs to be included (which works, but it’s not the standard or expected method)

The way to get around this is to have the header file call the definition file so that when the user includes the header file in the standard way, they also get the definition file. This preserves the consistency of inclusion while still allowing the template functionality to work.

More on this in the next set of flash cards.

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

True or False: If templates are present in any of the files to be linked, the compiler requires that a declaration AND a definition exist for the templates.

A

True

In a program that has #include “mytemplates.h” for example, mytemplates.h needs to either have the definitions for the templates or mytemplates.h needs to call mytemplates.cpp or whatever the definition file is so that the compiler can link.

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

Where are all parameter values, function variables, and return values allocated for templates?

A

The function call stack.

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

template <typename T, typename S>
S Func (T x, T y, S z)
{

}

What types can parameters x, y, and z be?

A

Any of the declared types for the template. In this case, x, y, and z can be of type T or S.

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

Do templates need to have overloads for the parameters they take in for any operators that are in the template?

A

Yes, unless the parameters being taken in are the primitive types (int, double, etc.) that already have defined operators by default.

template <typename T, typename S>
T Add(T x, S y)
{
return x + y;
}

The above needs an overload for the + operator that works with the T and S types, otherwise the respective x and y values cannot be added together.

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

True or False: Anything you can do with a C macro, you can do safely with a template.

A

True.

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

What is a C++ standard library feature class that manages dynamic arrays?

A

A vector, aka std::vector

17
Q

bool SimpleList<T>::Input<T>;</T></T>

How would the compiler create an integer version of this function?

A

bool SimpleList<int>::Input<int>;</int></int>

This would be the same for any other data type that is a valid type for the SimpleList template. All the T variables would be replaced with the type in every created function.

18
Q

Can the compiler compile files separately and then link them (the normal compilation process) when templates are present?

A

No.

You must:
Put the declarations in the .h file, then define them all at the end of the .h file.
-or-
Put the definitions in a .hpp file which is called at the bottom of the .h file. Use preprocessing directives to conditionally compile the .h file to prevent multiple definitions. (look at Conditional Compilation for more information)
-or-
Have the user #include the .hpp file instead of the .h file, but this is not intuitive to users.