Deck 16: Templates

Full screen (f)
exit full mode
Question
In implementing class template member functions,the functions are themselves templates.
Use Space or
up arrow
down arrow
to flip the card.
Question
The template prefix can be written template or template with the same results.
Question
What if anything is wrong with the following code?
template
class A
{
public:
A();
...
private:
...
};
A::A()
{
//...
}
Question
A non-template class can be derived from a class template instantiation.
Question
Templates can make available to the programmer the generality of algorithms that implementation with specific types conceals.
Question
Templates allow only parameterized types for class templates
Question
It is preferable to separate implementation and specification in software.Hence,it is preferable to place a template class definition in a "header" file,the template implementation of the member functions in an implementation file.The implementation should be compiled separately and linked to the application.
Question
To instantiate and call,a template function requires special syntax.
Question
What problems with templates have you encountered,or have you been warned about by your instructor,with regard to your compiler's template facility?
Question
In the template prefix,template the keyword class means that the type parameter T must be of class type.
Question
It is possible to have more than one type parameter in a template definition.
Question
Templates provide an overloading of the function for every possible type that is consistent with the use in the template.
Question
Templates allow only parameterized types for functions.
Question
A class template can be derived from a non-template class.
Question
In the implementation of a class template function,the class name before the scope resolution operator is just the class name,with no additional syntax.
Question
Suppose the swapValues template is instantiated as follows:
int x = 2,y =3;
swapValues(x,y);
// use x and y
x = 4;y =5;
swapValues(x,y);
// use x and y
The compiler generates code for two copies of the swapValues template.
Question
In the template prefix,template the identifier T is called a value parameter.
Question
In writing a class template,the function declarations within the class require special placement.
Question
A class template cannot be derived from a class template.
Question
With regard to programming usage,consistency in coding is more important than optimality.
Question
What is wrong with the following function template (Other than the fact that it really does almost nothing)?
template
int f( int & x )
{
return x;
}
int main()
{
int y = 3,z;
z = f(y);
}
Question
Discuss briefly,indicating whether mostly true or mostly false and justify:
A friend of a class is used exactly the same for template and non-template classes.
Question
There are three absolute value functions defined in various header files.These are abs,fabs,and labs.Write a template function that subsumes all three of these functions into one template function.
Question
Define or characterize the template facility for C++.
Question
Which of the following template function definitions and invocations will not compile,if any? If not,why not?
Assume that classes have been declared with appropriate default constructors and assume that
//a)
template
A func( A x,A y){ return A();}
int main()
{
U u1,u2,u3;
u2 = func(u2,u3);
}
//b.
template
B func(){ return 1;}
int main()
{
T t;
t = func();
}
//c.
template
void func(C x,int * y){}
int main()
{
T t;
int i;
func( t,&i );
}
//d.
template
void func(D x,E y){}
int main()
{
T t;
U u;
func ( t,u );
}
Question
Describe a strategy for writing template functions.
Question
Describe the syntax for a function template.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/27
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 16: Templates
1
In implementing class template member functions,the functions are themselves templates.
True
2
The template prefix can be written template or template with the same results.
True
3
What if anything is wrong with the following code?
template
class A
{
public:
A();
...
private:
...
};
A::A()
{
//...
}
The constructor A is a template function,hence it requires a template prefix:
template
A::A()
{
...
}
4
A non-template class can be derived from a class template instantiation.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
5
Templates can make available to the programmer the generality of algorithms that implementation with specific types conceals.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
6
Templates allow only parameterized types for class templates
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
7
It is preferable to separate implementation and specification in software.Hence,it is preferable to place a template class definition in a "header" file,the template implementation of the member functions in an implementation file.The implementation should be compiled separately and linked to the application.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
8
To instantiate and call,a template function requires special syntax.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
9
What problems with templates have you encountered,or have you been warned about by your instructor,with regard to your compiler's template facility?
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
10
In the template prefix,template the keyword class means that the type parameter T must be of class type.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
11
It is possible to have more than one type parameter in a template definition.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
12
Templates provide an overloading of the function for every possible type that is consistent with the use in the template.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
13
Templates allow only parameterized types for functions.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
14
A class template can be derived from a non-template class.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
15
In the implementation of a class template function,the class name before the scope resolution operator is just the class name,with no additional syntax.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
16
Suppose the swapValues template is instantiated as follows:
int x = 2,y =3;
swapValues(x,y);
// use x and y
x = 4;y =5;
swapValues(x,y);
// use x and y
The compiler generates code for two copies of the swapValues template.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
17
In the template prefix,template the identifier T is called a value parameter.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
18
In writing a class template,the function declarations within the class require special placement.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
19
A class template cannot be derived from a class template.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
20
With regard to programming usage,consistency in coding is more important than optimality.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
21
What is wrong with the following function template (Other than the fact that it really does almost nothing)?
template
int f( int & x )
{
return x;
}
int main()
{
int y = 3,z;
z = f(y);
}
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
22
Discuss briefly,indicating whether mostly true or mostly false and justify:
A friend of a class is used exactly the same for template and non-template classes.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
23
There are three absolute value functions defined in various header files.These are abs,fabs,and labs.Write a template function that subsumes all three of these functions into one template function.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
24
Define or characterize the template facility for C++.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
25
Which of the following template function definitions and invocations will not compile,if any? If not,why not?
Assume that classes have been declared with appropriate default constructors and assume that
//a)
template
A func( A x,A y){ return A();}
int main()
{
U u1,u2,u3;
u2 = func(u2,u3);
}
//b.
template
B func(){ return 1;}
int main()
{
T t;
t = func();
}
//c.
template
void func(C x,int * y){}
int main()
{
T t;
int i;
func( t,&i );
}
//d.
template
void func(D x,E y){}
int main()
{
T t;
U u;
func ( t,u );
}
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
26
Describe a strategy for writing template functions.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
27
Describe the syntax for a function template.
Unlock Deck
Unlock for access to all 27 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 27 flashcards in this deck.