here - Chara - University of Illinois at Urbana-Champaign
here - Chara - University of Illinois at Urbana-Champaign
here - Chara - University of Illinois at Urbana-Champaign
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
CS 296-25-CAG: Lecture 04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
<strong>University</strong> <strong>of</strong> <strong>Illinois</strong> <strong>at</strong> <strong>Urbana</strong>-<strong>Champaign</strong><br />
February 25, 2013
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Agenda<br />
Today we will discuss:<br />
1 Announcements<br />
2 Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
3 Partial Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
4 Templ<strong>at</strong>e Templ<strong>at</strong>e Parameters<br />
5 Next Time
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Agenda<br />
1 Announcements<br />
2 Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
3 Partial Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
4 Templ<strong>at</strong>e Templ<strong>at</strong>e Parameters<br />
5 Next Time
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Announcements<br />
Quiz recap.
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Announcements<br />
Quiz recap.<br />
MP1 is due Wednesday (02/27) <strong>at</strong> 11:59pm on Bitbucket.
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Announcements<br />
Quiz recap.<br />
MP1 is due Wednesday (02/27) <strong>at</strong> 11:59pm on Bitbucket.<br />
Please make sure I have read access!
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Announcements<br />
Quiz recap.<br />
MP1 is due Wednesday (02/27) <strong>at</strong> 11:59pm on Bitbucket.<br />
Please make sure I have read access!<br />
MP2 is out! Due: Wednesday, March 6th <strong>at</strong> 11:59pm on<br />
Bitbucket. (yes, you will have two things due <strong>at</strong> once)
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Announcements<br />
Quiz recap.<br />
MP1 is due Wednesday (02/27) <strong>at</strong> 11:59pm on Bitbucket.<br />
Please make sure I have read access!<br />
MP2 is out! Due: Wednesday, March 6th <strong>at</strong> 11:59pm on<br />
Bitbucket. (yes, you will have two things due <strong>at</strong> once)<br />
Assignment page (will be): https://chara.cs.<br />
illinois.edu/cs296/assignment/mp2.html
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Agenda<br />
1 Announcements<br />
2 Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
3 Partial Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
4 Templ<strong>at</strong>e Templ<strong>at</strong>e Parameters<br />
5 Next Time
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Motiv<strong>at</strong>ion<br />
We typically use templ<strong>at</strong>es for writing generic programs...
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Motiv<strong>at</strong>ion<br />
We typically use templ<strong>at</strong>es for writing generic programs...<br />
...but wh<strong>at</strong> if we want to have a “special case”
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Motiv<strong>at</strong>ion<br />
We typically use templ<strong>at</strong>es for writing generic programs...<br />
...but wh<strong>at</strong> if we want to have a “special case”<br />
(Controversial) Example: vector
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Motiv<strong>at</strong>ion<br />
We typically use templ<strong>at</strong>es for writing generic programs...<br />
...but wh<strong>at</strong> if we want to have a “special case”<br />
(Controversial) Example: vector<br />
Traditional implement<strong>at</strong>ion wastes a lot <strong>of</strong> space.
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Motiv<strong>at</strong>ion<br />
We typically use templ<strong>at</strong>es for writing generic programs...<br />
...but wh<strong>at</strong> if we want to have a “special case”<br />
(Controversial) Example: vector<br />
Traditional implement<strong>at</strong>ion wastes a lot <strong>of</strong> space.<br />
Recall: a bool actually takes up a byte—can’t get smaller<br />
than th<strong>at</strong> with addressable memory
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Motiv<strong>at</strong>ion<br />
We typically use templ<strong>at</strong>es for writing generic programs...<br />
...but wh<strong>at</strong> if we want to have a “special case”<br />
(Controversial) Example: vector<br />
Traditional implement<strong>at</strong>ion wastes a lot <strong>of</strong> space.<br />
Recall: a bool actually takes up a byte—can’t get smaller<br />
than th<strong>at</strong> with addressable memory<br />
Can we do better?
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Original Implement<strong>at</strong>ion<br />
#include <br />
templ<strong>at</strong>e <br />
class vector {<br />
public:<br />
// purposefully not doing reference <strong>here</strong>...<br />
T oper<strong>at</strong>or[]( size_t i ) {<br />
return d<strong>at</strong>a_[ i ];<br />
}<br />
// other things<br />
priv<strong>at</strong>e:<br />
T * d<strong>at</strong>a_;<br />
};
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Original Implement<strong>at</strong>ion<br />
#include <br />
templ<strong>at</strong>e <br />
class vector {<br />
public:<br />
// purposefully not doing reference <strong>here</strong>...<br />
T oper<strong>at</strong>or[]( size_t i ) {<br />
return d<strong>at</strong>a_[ i ];<br />
}<br />
// other things<br />
priv<strong>at</strong>e:<br />
T * d<strong>at</strong>a_;<br />
};<br />
If T = bool, we are wasting an entire byte on th<strong>at</strong><br />
boolean value (which can, in theory, be represented as a<br />
single bit)
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Original Implement<strong>at</strong>ion<br />
#include <br />
templ<strong>at</strong>e <br />
class vector {<br />
public:<br />
// purposefully not doing reference <strong>here</strong>...<br />
T oper<strong>at</strong>or[]( size_t i ) {<br />
return d<strong>at</strong>a_[ i ];<br />
}<br />
// other things<br />
priv<strong>at</strong>e:<br />
T * d<strong>at</strong>a_;<br />
};<br />
If T = bool, we are wasting an entire byte on th<strong>at</strong><br />
boolean value (which can, in theory, be represented as a<br />
single bit)<br />
We want to special case, or specialize on T = bool to<br />
change the default behavior <strong>of</strong> the class!
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
Definition (Templ<strong>at</strong>e Specializ<strong>at</strong>ion)<br />
Templ<strong>at</strong>e Specializ<strong>at</strong>ion is a mechanism by which the<br />
behavior <strong>of</strong> a templ<strong>at</strong>e class or function is completely replaced<br />
with something else, based on a condition on one or more<br />
templ<strong>at</strong>e parameters.
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
Definition (Templ<strong>at</strong>e Specializ<strong>at</strong>ion)<br />
Templ<strong>at</strong>e Specializ<strong>at</strong>ion is a mechanism by which the<br />
behavior <strong>of</strong> a templ<strong>at</strong>e class or function is completely replaced<br />
with something else, based on a condition on one or more<br />
templ<strong>at</strong>e parameters.<br />
A “gotcha”: when specializing, you replace the entire<br />
function/class
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
Definition (Templ<strong>at</strong>e Specializ<strong>at</strong>ion)<br />
Templ<strong>at</strong>e Specializ<strong>at</strong>ion is a mechanism by which the<br />
behavior <strong>of</strong> a templ<strong>at</strong>e class or function is completely replaced<br />
with something else, based on a condition on one or more<br />
templ<strong>at</strong>e parameters.<br />
A “gotcha”: when specializing, you replace the entire<br />
function/class<br />
When we specialize vector, we’ll have to write the<br />
entire class again.
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
Definition (Templ<strong>at</strong>e Specializ<strong>at</strong>ion)<br />
Templ<strong>at</strong>e Specializ<strong>at</strong>ion is a mechanism by which the<br />
behavior <strong>of</strong> a templ<strong>at</strong>e class or function is completely replaced<br />
with something else, based on a condition on one or more<br />
templ<strong>at</strong>e parameters.<br />
A “gotcha”: when specializing, you replace the entire<br />
function/class<br />
When we specialize vector, we’ll have to write the<br />
entire class again.<br />
Lesson: <strong>at</strong>tempt to specialize as little as possible—if only<br />
one or two functions are going to change in your<br />
specializ<strong>at</strong>ion, just specialize those functions as opposed<br />
to the class itself
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Example<br />
#include <br />
#include <br />
templ<strong>at</strong>e <br />
class vector {<br />
public:<br />
bool oper<strong>at</strong>or[]( size_t i ) {<br />
size_t idx = i / 8;<br />
// I hope this is correct...<br />
return ( d<strong>at</strong>a_[ idx ] >> ( 7 - ( i % 8 ) ) ) & 1;<br />
}<br />
// other things<br />
priv<strong>at</strong>e:<br />
uint8_t * d<strong>at</strong>a_;<br />
};
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Example<br />
#include <br />
#include <br />
templ<strong>at</strong>e <br />
class vector {<br />
public:<br />
bool oper<strong>at</strong>or[]( size_t i ) {<br />
size_t idx = i / 8;<br />
// I hope this is correct...<br />
return ( d<strong>at</strong>a_[ idx ] >> ( 7 - ( i % 8 ) ) ) & 1;<br />
}<br />
// other things<br />
priv<strong>at</strong>e:<br />
uint8_t * d<strong>at</strong>a_;<br />
};<br />
With this specializ<strong>at</strong>ion, storing a collection <strong>of</strong> boolean<br />
values can be made more space efficient
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Example Cont’d<br />
#include <br />
#include <br />
templ<strong>at</strong>e <br />
class vector {<br />
public:<br />
bool oper<strong>at</strong>or[]( size_t i ) {<br />
size_t idx = i / 8;<br />
// I hope this is correct...<br />
return ( d<strong>at</strong>a_[ idx ] >> ( 7 - ( i % 8 ) ) ) & 1;<br />
}<br />
// other things<br />
priv<strong>at</strong>e:<br />
uint8_t * d<strong>at</strong>a_;<br />
};<br />
The decision to keep the same interface was arbitrary
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Example Cont’d<br />
#include <br />
#include <br />
templ<strong>at</strong>e <br />
class vector {<br />
public:<br />
bool oper<strong>at</strong>or[]( size_t i ) {<br />
size_t idx = i / 8;<br />
// I hope this is correct...<br />
return ( d<strong>at</strong>a_[ idx ] >> ( 7 - ( i % 8 ) ) ) & 1;<br />
}<br />
// other things<br />
priv<strong>at</strong>e:<br />
uint8_t * d<strong>at</strong>a_;<br />
};<br />
The decision to keep the same interface was arbitrary<br />
No requirement to do so! In fact, it may make sense to<br />
have a different interface for your specializ<strong>at</strong>ions.
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
A More Reasonable Example<br />
Suppose we want to put new class we’ve made (MyClass)<br />
into an unordered map or unordered set
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
A More Reasonable Example<br />
Suppose we want to put new class we’ve made (MyClass)<br />
into an unordered map or unordered set<br />
We must define wh<strong>at</strong> it means to hash our new class
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
A More Reasonable Example<br />
Suppose we want to put new class we’ve made (MyClass)<br />
into an unordered map or unordered set<br />
We must define wh<strong>at</strong> it means to hash our new class<br />
#include <br />
namespace std {<br />
templ<strong>at</strong>e <br />
struct hash {<br />
size_t oper<strong>at</strong>or()( const MyClass & c ) const {<br />
size_t ret = /* do something awesome <strong>here</strong> */ ;<br />
// and probably <strong>here</strong><br />
return ret;<br />
}<br />
};<br />
}
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Agenda<br />
1 Announcements<br />
2 Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
3 Partial Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
4 Templ<strong>at</strong>e Templ<strong>at</strong>e Parameters<br />
5 Next Time
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Motiv<strong>at</strong>ion<br />
Templ<strong>at</strong>e specializ<strong>at</strong>ion allows us to define custom<br />
behavior for a specific type
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Motiv<strong>at</strong>ion<br />
Templ<strong>at</strong>e specializ<strong>at</strong>ion allows us to define custom<br />
behavior for a specific type<br />
...but wh<strong>at</strong> if we want to define custom behavior for a<br />
collection <strong>of</strong> types (e.g. pointers)?
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Motiv<strong>at</strong>ion<br />
Templ<strong>at</strong>e specializ<strong>at</strong>ion allows us to define custom<br />
behavior for a specific type<br />
...but wh<strong>at</strong> if we want to define custom behavior for a<br />
collection <strong>of</strong> types (e.g. pointers)?<br />
Example: Imagine we wanted vector to take<br />
ownership <strong>of</strong> the int * inserted?
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Motiv<strong>at</strong>ion<br />
Templ<strong>at</strong>e specializ<strong>at</strong>ion allows us to define custom<br />
behavior for a specific type<br />
...but wh<strong>at</strong> if we want to define custom behavior for a<br />
collection <strong>of</strong> types (e.g. pointers)?<br />
Example: Imagine we wanted vector to take<br />
ownership <strong>of</strong> the int * inserted?<br />
We want delete to be called on each item <strong>of</strong> the<br />
collection during destruction—this won’t happen with our<br />
ordinary definition.
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Motiv<strong>at</strong>ion<br />
Templ<strong>at</strong>e specializ<strong>at</strong>ion allows us to define custom<br />
behavior for a specific type<br />
...but wh<strong>at</strong> if we want to define custom behavior for a<br />
collection <strong>of</strong> types (e.g. pointers)?<br />
Example: Imagine we wanted vector to take<br />
ownership <strong>of</strong> the int * inserted?<br />
We want delete to be called on each item <strong>of</strong> the<br />
collection during destruction—this won’t happen with our<br />
ordinary definition.<br />
Adding a specializ<strong>at</strong>ion for every pointer type we use isn’t<br />
feasible
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Original Implement<strong>at</strong>ion<br />
templ<strong>at</strong>e <br />
class vector {<br />
public:<br />
~vector() {<br />
delete [] d<strong>at</strong>a_;<br />
}<br />
// other things<br />
priv<strong>at</strong>e:<br />
T * d<strong>at</strong>a_;<br />
};<br />
int main() {<br />
vector a;<br />
a.push_back( new int(5) );<br />
// oops! memory leak!<br />
}
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Modified Implement<strong>at</strong>ion<br />
templ<strong>at</strong>e <br />
class vector {<br />
public:<br />
~vector() {<br />
for( size_t i = 0; i < size_; ++i )<br />
delete ptr;<br />
delete [] d<strong>at</strong>a_;<br />
}<br />
// other things<br />
priv<strong>at</strong>e:<br />
T ** d<strong>at</strong>a_;<br />
};<br />
int main() {<br />
vector a;<br />
a.push_back( new int(5) );<br />
// no more memory leak!<br />
}
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Modified Implement<strong>at</strong>ion (Cont’d.)<br />
templ<strong>at</strong>e <br />
class vector {<br />
public:<br />
~vector() {<br />
for( size_t i = 0; i < size_; ++i )<br />
delete ptr;<br />
delete [] d<strong>at</strong>a_;<br />
}<br />
// other things<br />
priv<strong>at</strong>e:<br />
T ** d<strong>at</strong>a_;<br />
};<br />
Note th<strong>at</strong> T still refers to int in an instanti<strong>at</strong>ion <strong>of</strong><br />
vector, so we need the T ** declar<strong>at</strong>ion <strong>of</strong><br />
d<strong>at</strong>a .
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Partial Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
Not restricted to the pointer case—can specialize for one<br />
<strong>of</strong> many templ<strong>at</strong>e parameters.
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Partial Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
Not restricted to the pointer case—can specialize for one<br />
<strong>of</strong> many templ<strong>at</strong>e parameters.<br />
templ<strong>at</strong>e <br />
struct Spec {<br />
void f() { cout
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Agenda<br />
1 Announcements<br />
2 Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
3 Partial Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
4 Templ<strong>at</strong>e Templ<strong>at</strong>e Parameters<br />
5 Next Time
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Motiv<strong>at</strong>ion<br />
When instanti<strong>at</strong>ing templ<strong>at</strong>es, you typically pass in a<br />
well-defined class as a templ<strong>at</strong>e parameter (e.g.<br />
vector).
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Motiv<strong>at</strong>ion<br />
When instanti<strong>at</strong>ing templ<strong>at</strong>es, you typically pass in a<br />
well-defined class as a templ<strong>at</strong>e parameter (e.g.<br />
vector).<br />
Wh<strong>at</strong> if you wanted to pass in a templ<strong>at</strong>e, and instanti<strong>at</strong>e<br />
it yourself in your templ<strong>at</strong>e function/class?
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Motiv<strong>at</strong>ion<br />
When instanti<strong>at</strong>ing templ<strong>at</strong>es, you typically pass in a<br />
well-defined class as a templ<strong>at</strong>e parameter (e.g.<br />
vector).<br />
Wh<strong>at</strong> if you wanted to pass in a templ<strong>at</strong>e, and instanti<strong>at</strong>e<br />
it yourself in your templ<strong>at</strong>e function/class?<br />
Example: I want to instanti<strong>at</strong>e a container with multiple<br />
different parameters (like having two unordered maps<br />
with different key/value types).
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Original Implement<strong>at</strong>ion<br />
templ<strong>at</strong>e <br />
class my_class {<br />
public:<br />
// things<br />
priv<strong>at</strong>e:<br />
HT1 first_;<br />
HT2 second_;<br />
};<br />
typedef simple_hash_table int_double_map;<br />
typedef simple_hash_table string_int_map;<br />
my_class m;
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Example<br />
templ<strong>at</strong>e <br />
class my_class {<br />
public:<br />
// things<br />
priv<strong>at</strong>e:<br />
HT first_;<br />
HT second_;<br />
};<br />
my_class m;<br />
I’m glossing over using STL containers as<br />
templ<strong>at</strong>e-templ<strong>at</strong>e parameters on purpose—they have an<br />
additional templ<strong>at</strong>e parameter you’d have to deal with<br />
(not hard, but not worth discussing in-band).
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Agenda<br />
1 Announcements<br />
2 Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
3 Partial Templ<strong>at</strong>e Specializ<strong>at</strong>ion<br />
4 Templ<strong>at</strong>e Templ<strong>at</strong>e Parameters<br />
5 Next Time
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Coming up...<br />
MP1! Please start now if you haven’t—it’s hard (and,<br />
moreover, harder than I intended)!
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Coming up...<br />
MP1! Please start now if you haven’t—it’s hard (and,<br />
moreover, harder than I intended)!<br />
MP2! Please start as soon as you can (I’ll release it<br />
tonight, for serious this time). It should be a nice bre<strong>at</strong>her.
Lecture<br />
04—Templ<strong>at</strong>e<br />
P<strong>at</strong>terns I<br />
Chase Geigle<br />
Announcements<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Partial<br />
Templ<strong>at</strong>e<br />
Specializ<strong>at</strong>ion<br />
Templ<strong>at</strong>e<br />
Templ<strong>at</strong>e<br />
Parameters<br />
Next Time<br />
Coming up...<br />
MP1! Please start now if you haven’t—it’s hard (and,<br />
moreover, harder than I intended)!<br />
MP2! Please start as soon as you can (I’ll release it<br />
tonight, for serious this time). It should be a nice bre<strong>at</strong>her.<br />
Next lecture we’ll talk about Dynamic vs St<strong>at</strong>ic<br />
Polymorphism, and using the Curiously Recurring<br />
Templ<strong>at</strong>e P<strong>at</strong>tern