Week 9 - Gemini Flashcards
(17 cards)
What is the primary purpose of using ‘templates’ in C++ (both function and class templates)?
Templates allow for ‘generic programming’ by enabling the creation of functions and classes that can operate with different data types without having to rewrite the code for each type. They act as blueprints.
What is a ‘function template’? Describe its basic syntax.
A function template is a blueprint for creating a family of functions that perform the same operations but on different data types. Syntax: template <typename T> return_type function_name(T parameter1, T parameter2, ...){ /* function body */ }
(or template <class T>
).
In a function template like template <typename T> T myMin(T a, T b)
, what does T
represent?
T
is a ‘template parameter’ or ‘type parameter’. It acts as a placeholder for an actual data type (e.g., int
, double
, std::string
) that will be specified or deduced when the function is called.
How does the compiler typically determine the actual data type for a template parameter T
when a function template is called (e.g., myMin(3, 5)
)?
The compiler deduces the type T
from the types of the arguments passed to the function. In myMin(3, 5)
, T
would be deduced as int
.
What happens if you call a function template with arguments of mixed types that the template cannot resolve to a single T
, e.g., myMin(3, 5.0)
for template <typename T> T myMin(T a, T b)
?
This can lead to a compilation error because the compiler cannot deduce a single consistent type for T
. T
would be ambiguous (is it int
or double
?). You might need explicit template arguments or ensure arguments are of the same type.
Can a function template have multiple type parameters? Give a conceptual syntax.
Yes. Conceptual syntax: template <typename T1, typename T2> ReturnType function_name(T1 param1, T2 param2) { /* ... */ }
.
What is a ‘class template’? Describe its basic purpose.
A class template is a blueprint for creating a family of classes that have the same structure and member functions but can operate on different data types specified as template parameters. It allows for generic data structures and classes.
What is the basic syntax for declaring a class template named MyCollection
that can hold elements of a generic type T
?
template <typename T> class MyCollection { private: T* data; int size; public: /* constructors, methods using T */ };
How do you instantiate an object of a class template MyCollection
to hold integers?
MyCollection<int> intCollection;
The actual type (int
) is specified within angle brackets.
If a member function of a class template is defined outside the class declaration, what special syntax is required for its definition?
The member function definition must also be a template and be prefixed with template <typename T>
(or whatever template parameters the class uses), and the class name must be qualified with the template parameter, e.g., template <typename T> void MyCollection<T>::add(T item) { /* ... */ }
.
Why are the definitions of template classes and their member functions (if not defined inline) usually placed entirely in header (.h
or .hpp
) files?
Because the compiler needs the full template definition (not just the declaration) at the point where the template is instantiated with a specific type. If definitions were in a .cpp
file, the compiler wouldn’t see them when compiling another .cpp
file that uses the template, leading to linking errors.
Consider a class template BinLattice<Type>
designed to store a 2D lattice where Type
can be double
for prices or bool
for a stopping policy. How does this demonstrate the power of class templates?
It allows the same underlying data structure and access logic (e.g., SetNode
, GetNode
) to be reused for different data types without duplicating the class code. One generic BinLattice
definition serves multiple purposes.
If BinLattice<Type>
has a member std::vector<std::vector<Type>> Lattice;
, how does Type
affect this member?
Type
determines the kind of data stored in the innermost vectors. If Type
is double
, it’s a vector of vectors of doubles. If Type
is bool
, it’s a vector of vectors of bools.
If BinLattice<Type>
has a function void SetNode(int n, int i, Type x);
, how does the template parameter Type
influence this function’s usage?
The third parameter x
must be of the same type as Type
with which the BinLattice
object was instantiated. For BinLattice<double> prices;
, x
must be a double
. For BinLattice<bool> policy;
, x
must be a bool
.
Can std::vector
itself be considered an example of a class template? Explain briefly.
Yes, std::vector
is a class template from the C++ Standard Library. You use it like std::vector<int>
, std::vector<double>
, or std::vector<MyClass>
, where the type in the angle brackets is the template argument specifying what type of elements the vector will hold.
What is ‘generic programming’ and how do templates facilitate it?
Generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters. Templates are the C++ mechanism for achieving this, allowing code to be written once and used with various data types.
When designing a function template that performs an operation (e.g., comparison with <
as in myMin
), what is an implicit assumption about the types T
that can be used with it?
The template assumes that the operations used within its body (like <
for myMin
, or +
if it were a sum template) are valid and defined for any type T
with which the template is instantiated. If T
doesn’t support those operations, a compile error will occur.