Chapter 16.1.1 - Function Templates

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What types can a nontype parameter be?

They can be of integral type or a pointer or lvalue reference to an object or to a function type.

In order to call a function, the compiler only needs to see a declaration for that function. Likewise for classes, the compiler only needs to see a class definition to use it; it doesn't need to see it's member function definitions. As a result of this, we usually put class definitions and function declarations in headers files and definitions of ordinary and class members functions in a separate source file. Unlike the above examples, headers for templates should include both the definitions and declarations. Why is this so?

This is due to the fact that in order to instantiate, the compiler needs to have the code that defines a function template or class template member function. I.E you can't have them in separate files.

Nontype parameters that are pointers can take a nullptr or a zero valued constant expression as an argument. T/F?

True.

What's wrong with the following function template declaration? How would you fix it? inline template<typename T> T min(const T&, const T&);

When declaring a function template as inline, the "inline" keyword should go before the function's return type (the same way you do with non template functions)

Explain what it means when the compiler "instantiates" a function template.

When instantiating a function template, the compiler creates a specific version of the template replacing the template parameters with template arguments.

What should you try to minimize when creating template programs?

You should try to minimize the number of requirements placed on the argument types. Example: reducing the number of operators used and/or using more general and accommodating operators (for example using the "less" function instead of the < operator)

Given the following template function: template <typename T> int compare(const T& v1, const T& v2) { //Function body } With the following function call, what type will the compiler bind to template parameter 'T'? compare(1, 0);

int

A type parameter in a template parameter list must be preceded by the "class" or "typename" keywords. What are the differences between these two?

"class" and "typename" have the same meaning and can be used interchangeably inside a template parameter list.

What is a function template?

A function template is a formula from which we can generate type specific versions of a function.

What is a nontype parameter and how do we specify one?

A nontype parameter is a kind of template parameter that represents a value rather than a type (like type parameters do). A nontype parameter is specified by using a specific type name instead of the "class" or "typename" keywords.

What does a template definition consist of?

A template definition starts with the keyword "template" followed by a template parameter list, which is a comma separated list of template parameters surround by <> brackets. Example: template<typename T, typename U>

In what ways can a type parameter be used?

A type parameter can be used as a type specifier in the same way that we use a built in or class type specifier. In particular we use a type parameter to: - Name the return type - Name function parameter types - Variable declarations and casts inside the function body.

What is a type parameter?

A type parameter is a name used in a template parameter list to represent a type. A type parameter must be preceded by either the "typename" or "class" keywords. For example in, template<typename T, typename U>, 'T' and 'U' are type parameters

When writing generic code for a template function, why is it important to have function parameters references to const?

By doing this, we ensure that our template function can be used with types that cannot be copied. In addition to this, we can avoid the overhead of copying potentially large objects.

How are template arguments in a function template implicitly specified?

If you don't explicitly specify any template arguments, the compiler will use the function arguments to deduce the template arguments for us.

What does a user who calls a template have to guarantee?

It is up to the caller to guarantee that the arguments passed support any operations that the template uses and that those operations behave correctly in the context in which the template uses them.

Given the following template function: template<typename T, size_t N> void Print(const T(&p)[N]) { //Function Body } Given the following code, when the compiler instantiates Print, what will be value of nontype parameter N? int i[] = {1, 2, 3}; Print(i);

N will be 3 because the compiler deduces the size of array i.

In a template definition, can a template parameter list be empty?

No, a template parameter list cannot be empty

When does the compiler generate code for a template? What does this affect?

The compiler generates code only when we instantiate a template. It does not generate code when it sees the definition of a template. This affects how we organize our code and when errors are detected.

Given the following template function: template <typename T> int compare(const T& v1, const T& v2) { //Function body } What kind of function is instantiated with the following call to compare? vector<int> vect1{1, 2, 3}, vect2{3, 4, 5}; compare(vect1, vect2);

The compiler will instantiate: int compare(const vector<int>&, const vector<int>&)

What is the first stage in which the compiler might flag an error for a template? What kind of errors can it detect?

The first stage is when we compile the template itself. The compiler can only detect syntax errors at this point.

What is the second stage in which the compiler might flag an error for a template? What kind of errors can it detect?

The second stage is when the compiler sees use of the template. For function templates, the compiler can detect errors related to whether an appropriate number of arguments are passed. It can also detect whether two arguments that are supposed to have the the same type do so. For class templates, the compiler can check that the right number of template arguments are provided.

What is the third stage in which the compiler might flag an error for a template? What kind of errors can it detect?

The third stage is during instantiation of the template. It is only in this stage that type-related errors can be found.

What's wrong with the following template function? template<typename T, U> T calc (const T&, const U&)

The type parameter 'U' isn't preceded by either the "class" or "typename" keyword.

What restrictions do the values supplied to a nontype parameter have?

These values must be constant expressions. If the non type parameter is a reference or pointer, then the argument bound to that parameter must have a static lifetime; they cannot be ordinary local objects or a dynamic object.

Do template arguments have to be specified implicitly or explicitly?

They are allowed to be specified either way

What are nontype parameters replaced with when a template is instantiated?

They are replaced with values supplied by the user or deduced by the compiler.


संबंधित स्टडी सेट्स

Chapter 11 Homework: Cardiovascular System

View Set

Toxicity - Which vitamin and level of toxicity is the symptom associated with?

View Set

PHIL 100 CSU Alvarez, exam 5, Final

View Set

Cell and molecular biology quizzes

View Set