Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments...
Transcript of Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments...
![Page 1: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/1.jpg)
Lecture 7: Template FunctionsCS 106L, Fall ‘20
1
![Page 2: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/2.jpg)
Today’s Agenda
• Recap: Iterators• Template functions• Announcements• (supplemental materials) Variadic Templates• Concept Lifting
2
![Page 3: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/3.jpg)
Recap: Iterators
3
![Page 4: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/4.jpg)
Iterators allow iteration over any containerwhether ordered or unordered
![Page 5: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/5.jpg)
std::set<T> s;auto iter = s.begin();iter++; // increment; prefix operator is faster (why?)*iter; // dereference iter to get curr value(iter != s.end()); // equality comparison
iter = another_iter; // copy construction
STL Iterators
Generally, STL iterators support the following operations:
s.begin(); // an iterator pointing to the first elements.end(); // one past the last element
STL collections have the following operations:
![Page 6: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/6.jpg)
std::set<int> set {3, 1, 4, 1, 5, 9};for (initialization; termination-condition; increment) { const auto& elem = retrieve-element; cout << elem << endl;}
std::map<int> map {{1, 6}, {1, 8}, {0, 3}, {3, 9}};for (initialization; termination-condition; increment) { const auto& [key, value] = retrieve-element; // structured binding! cout << key << “:” << value << endl;}
Printing all elements in these collections
![Page 7: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/7.jpg)
std::set<int> set {3, 1, 4, 1, 5, 9};for (auto iter = set.begin(); iter != set.end(); ++iter) { const auto& elem = *iter; cout << elem << endl;}
std::map<int> map {{1, 6}, {1, 8}, {0, 3}, {3, 9}};for (auto iter = map.begin(); iter != map.end(); ++iter) { const auto& [key, value] = *iter; // structured binding! cout << key << “:” << value << endl;}
Printing all elements in these collections
![Page 8: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/8.jpg)
std::set<int> set {3, 1, 4, 1, 5, 9};for (const auto& elem : set) { cout << elem << endl;}
std::map<int> map {{1, 6}, {1, 8}, {0, 3}, {3, 9}};for (const auto& [key, value] : map) { cout << key << “:” << value << endl;}
Another option: for-each loops!
For-each loops use iterators under the hood!
![Page 9: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/9.jpg)
🤔 Questions? 🤔
![Page 10: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/10.jpg)
Template Functions
10
![Page 11: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/11.jpg)
11
int my_min(int a, int b) { return a < b ? a : b;}
if conditionreturn if true return if false
// equivalentlyint my_min(int a, int b) { if (a < b) return a; else return b;}
Sidenote: Ternary Operator
int my_min(int a, int b) { return a < b ? a : b;}
// equivalently
int my_min(int a, int b) {
if (a < b) return a;
else return b;
}
if condition return if true return if false
![Page 12: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/12.jpg)
12
int main() { auto min_int = my_min(1, 2); auto min_name = my_min(“Anna”, “Avery”);}
Can we handle different types?
int main() { auto min_int = my_min(1, 2); auto min_name = my_min(“Nikhil”, “Ethan”);}
![Page 13: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/13.jpg)
One way: overloaded functions
13
int my_min(int a, int b) { return a < b ? a : b;}
std::string my_min(std::string a, std::string b) { return a < b ? a : b;}
![Page 14: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/14.jpg)
One way: overloaded functions
14
int my_min(int a, int b) { return a < b ? a : b;}
std::string my_min(std::string a, std::string b) { return a < b ? a : b;}
Bigger problem: how do we handle user-defined types?
![Page 15: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/15.jpg)
15
template <typename T>T my_min(T a, T b) { return a < b ? a : b;}
We now have a generic function!
template <typename T>T my_min(T a, T b) { return a < b ? a : b;}
![Page 16: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/16.jpg)
16
template <typename T>T my_min(T a, T b) { return a < b ? a : b;}
Template function syntax analysis
template <typename T>T my_min(T a, T b) { return a < b ? a : b;}
Declares the next declaration is a
template
Specifies T is some arbitrary type
List of template arguments
Note: Scope of template argument T is limited to this
one function!
![Page 17: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/17.jpg)
Just in case we don’t want to copy T
17
template <typename T>T my_min(T a, T b) { return a < b ? a : b;}
template <typename T>T my_min(const T& a, const T& b) { return a < b ? a : b;}
![Page 18: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/18.jpg)
Live Code Demo:Templates: syntax and initialization
![Page 19: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/19.jpg)
There are two ways to call template functions!
19
template <typename T>T my_min(const T& a, const T& b) { return a < b ? a : b;}
![Page 20: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/20.jpg)
Way 1: Explicit instantiation of templates
20
template <typename T>T my_min(const T& a, const T& b) { return a < b ? a : b;}
my_min<string>(“Nikhil”, “Ethan”);
Explicitly states T = string
Compiler replaces every T with string
![Page 21: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/21.jpg)
Way 2: Implicit instantiation of templates
21
template <typename T>T my_min(const T& a, const T& b) { return a < b ? a : b;}
my_min(3, 4);
Compiler deduces T = int
Compiler replaces every T with int
![Page 22: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/22.jpg)
Be careful: type deduction can’t read your mind!
22
template <typename T>T my_min(const T& a, const T& b) { return a < b ? a : b;}
my_min(“Nikhil”, “Ethan”);
Compiler deduces T = char* (C-string)
Compiler replaces every T with char*
Comparing pointers -- not what you
want!
![Page 23: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/23.jpg)
Our function isn’t technically correct
23
template <typename T>T my_min(const T& a, const T& b) { return a < b ? a : b;}
my_min(4, 3.2);// this returns 3
Compiler deduces T = int
![Page 24: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/24.jpg)
Be careful: type deduction can’t read your mind!
24
template <typename T, typename U>auto my_min(const T& a, const U& b) { return a < b ? a : b;}
my_min(4, 3.2);// this returns 3.2
Accounting for the fact that the types could be different
The return type is kind of complicated, so let the compiler
figure it out
![Page 25: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/25.jpg)
You can overload non-template special cases
25
char* my_min(const char*& a, const char*& b) { return std::string(a) < std::string(b) ? a : b;}
template <typename T, typename U>auto my_min(const T& a, const U& b) { return a < b ? a : b;}
If we get C-strings, run this special case
Otherwise, create a template
![Page 26: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/26.jpg)
Template Instantiation: creating an “instance” of your template
26
When you call a template function, either:
● for explicit instantiation, compiler finds the relevant template and creates that
function in the executable.
● for implicit instantiation, compiler looks at all possible overloads (template and
non-template), picks the best one, deduces the template parameters, and creates
that function in the executable.
● After instantiation, the compiled code looks as if you had written the instantiated
version of the function yourself.
![Page 27: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/27.jpg)
Template functions are not functions
They’re a recipe for generating functions via instantiation.
27
![Page 28: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/28.jpg)
🤔 Questions? 🤔
![Page 29: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/29.jpg)
Announcements
29
![Page 30: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/30.jpg)
Assignment 1 Will Be Released Tomorrow!
30
● Due Sunday, October 25 on Paperless● There will be a very small warm-up due next week● We’ll send out an announcement with all logistical details
![Page 31: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/31.jpg)
Supplemental Material: Variadic Templates *
31
* not essential information to understand to make it through the rest of this class -- we’re including this topic just because it’s cool :)
Supplemental Material
![Page 32: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/32.jpg)
How can we make this function even more generic?
32
int main() { auto min1 = my_min(4.2, -7.9);
}
Supplemental Material
![Page 33: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/33.jpg)
Say, an arbitrary number of parameters?
33
int main() { auto min1 = my_min(4.2, -7.9); auto min2 = my_min(4.2, -7.9, 8.223); auto min3 = my_min(4.2, -7.9, 8.223, 0.0); auto min4 = my_min(4.2, -7.9, 8.223, 0.0, 1.753);}
Supplemental Material
![Page 34: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/34.jpg)
Take a moment to think about this
// assume nums is non-empty and T is comparabletemplate <typename T>T my_min(vector<T>& nums) {
}
How would you write a recursive version of my_min that accepts a vector?
Supplemental Material
![Page 35: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/35.jpg)
Take a moment to think about this
// assume nums is non-empty and T is comparabletemplate <typename T>T my_min(vector<T>& nums) { T elem = nums[0]; if (nums.size() == 1) return elem; auto min = my_min(nums.subList(1)); if (elem < min) min = elem; return min;}
How would you write a recursive version of my_min that accepts a vector?
Supplemental Material
![Page 36: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/36.jpg)
template <typename T, typename ...Ts>auto my_min(T num, Ts... args) { auto min = my_min(args...); if (num < min) min = num; return min;}
Let’s translate this into a variadic template!
Variadic templates can use compile-time recursion
Supplemental Material
![Page 37: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/37.jpg)
template <typename T, typename ...Ts>auto my_min(T num, Ts... args) { auto min = my_min(args...); if (num < min) min = num; return min;}
Let’s translate this into a variadic template!
Variadic templates can use compile-time recursion
Pack expansion: comma-separated
patterns
Parameter pack: 0 or more types
Supplemental Material
![Page 38: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/38.jpg)
// expands to f(&E1, &E2, &E3)f(&args...);
// expands to f(n, ++E1, ++E2, ++E3);f(n, ++args...);
// expands to f(++E1, ++E2, ++E3, n);f(++args..., n);
// f(const_cast<const E1*>(&X1), const_cast<const E2*>(&X2), ...)f(const_cast<const Args*>(&args)...);
Parameter Pack expansion examples
38
Supplemental Material
![Page 39: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/39.jpg)
What does this expand to?
39
f(h(args...) + args...);
// f(h(E1,E2,E3) + E1, h(E1,E2,E3) + E2, h(E1,E2,E3) + E3)
Supplemental Material
![Page 40: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/40.jpg)
Don’t forget a base case!
40
template <typename T, typename ...Ts>auto my_min(const T& num, Ts... args) { auto min = my_min(args...); if (num < min) min = num; return min;}
template <typename T>auto my_min(const T& num) { return num;}
Supplemental Material
![Page 41: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/41.jpg)
Calls to the function are pattern-matched to these templates
template <typename T, typename ...Ts>pair<T, T> my_min(T num, Ts... args) { auto [min, max] = my_min(args...); if (num < min) min = num; if (num > max) max = num; return {min, max};}
This pattern-matching happens at compile time, not runtime
Supplemental Material
![Page 42: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/42.jpg)
Calls to the function are pattern-matched to these templates
template <typename T, typename ...Ts>pair<T, T> my_min(T num, Ts... args) { auto [min, max] = my_min(args...); if (num < min) min = num; if (num > max) max = num; return {min, max};}
This pattern-matching happens at compile time, not runtime
auto [min3, max3] = my_min(4.2, -7.9, 8.223, 0.0);
// T = int// Ts = int, int, int
// num = 4.2// args = -7.9, 8.223, 0.0
Supplemental Material
![Page 43: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/43.jpg)
The parameter pack is expanded into a comma-separated list
template <typename T, typename ...Ts>pair<T, T> my_min(T num, Ts... args) { auto [min, max] = my_min(args...); if (num < min) min = num; if (num > max) max = num; return {min, max};}
This pattern-matching happens at compile time, not runtime
Equivalent to:auto [min3, max3] = my_min(-7.9, 8.223, 0.0);
Since:// num = 4.2// args = -7.9, 8.223, 0.0
Supplemental Material
![Page 44: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/44.jpg)
template <typename T, typename ...Ts>void printf(string format, T value, Ts... args) { int pos = format.find(‘%’); if (pos == string::npos) return; cout << format.substr(0, pos) << value; printf(format.substr(pos+1), args...);}
void printf(string format) { cout << format;}
The types do not have to be homogeneous
Example: A simplified version of C’s printf function without format flags *
Supplemental Material
* no worries if you’re not familiar with the printf function. You’ll learn about this if you take CS 107! :)
![Page 45: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/45.jpg)
template <typename T, typename ...Ts>void printf(string format, T value, Ts... args) { … printf(format.substr(pos+1), args...);}
At compile time, the compiler instantiates the templates
printf(“Lecture %: % (Week %)”, 7, “Templates”s, 4);
First, using template deduction, we deduce T and Ts
Supplemental Material
![Page 46: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/46.jpg)
template <typename T, typename ...Ts>void printf(string format, T value, Ts... args) { … printf(format.substr(pos+1), args...);}
At compile time, the compiler instantiates the templates
printf(“Lecture %: % (Week %)”, 7, “Templates”s, 4);
printf<int, string, int> // this function is generated
A new function is generated that matches our function call . . .
Supplemental Material
![Page 47: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/47.jpg)
template <typename T, typename ...Ts>void printf(string format, int value, string arg1, int arg2) { … printf(format.substr(pos+1), arg1, arg2);}
At compile time, the compiler instantiates the templates
printf(“Lecture %: % (Week %)”, 7, “Templates”s, 4);
printf<int, string, int> // this function is generated
. . . and everything is replaced with the instantiated types!
Supplemental Material
![Page 48: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/48.jpg)
template <typename T, typename ...Ts>void printf(string format, T value, Ts... args) { … printf(format.substr(pos+1), args...);}
At compile time, the compiler instantiates the templates
printf<int, string, int>
printf<string, int> // this function is generated next
The recursive call tells us the next instantiation that we need
Supplemental Material
![Page 49: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/49.jpg)
template <typename T, typename ...Ts>void printf(string format, string value, int arg1) { … printf(format.substr(pos+1), arg1);}
At compile time, the compiler instantiates the templates
printf<int, string, int>
printf<string, int> // this function is generated next
Instantiation again replaces the types
Supplemental Material
![Page 50: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/50.jpg)
template <typename T, typename ...Ts>void printf(string format, T value, Ts... args) { … printf(format.substr(pos+1), args...);}
At compile time, the compiler instantiates the templates
printf<int, string, int>
printf<string, int>
printf<int> // this function is generated next
Again, the recursive call tells us the next instantiation that we need
Supplemental Material
![Page 51: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/51.jpg)
template <typename T, typename ...Ts>void printf(string format, string value) { … printf(format.substr(pos+1));}
At compile time, the compiler instantiates the templates
printf<int, string, int>
printf<string, int>
printf<int> // this function is generated next
Finally, the parameter pack is empty
Supplemental Material
![Page 52: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/52.jpg)
void printf(string format) { cout << format;}
At compile time, the compiler instantiates the templates
printf<int, string, int>
printf<string, int>
printf<int>
printf // this function is generated next
One more function is compiled: the non-template base function
Supplemental Material
![Page 53: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/53.jpg)
printf<int, string, int>
printf<string, int>
printf<int>
printf
At compile time, these functions are compiled
53
Supplemental Material
![Page 54: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/54.jpg)
Concept lifting
![Page 55: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/55.jpg)
What assumptions are we making about the parameters?
Can we solve a more general problem by relaxing some of the constraints?
![Page 56: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/56.jpg)
Why write generic functions?
Count the number of times 3 appears in a std::list<int>.Count the number of times “X” appears in a std::istream.Count the number of times a vowel appears in a std::string.Count the number of times a college student appears in a census.
![Page 57: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/57.jpg)
Remove as many assumptions as you can
![Page 58: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/58.jpg)
How many times does an int appear in a vector of ints?
int count_occurrences(const vector<int>& vec, int val) { int count = 0; for (size_t i = 0; i < vec.size(); i++) { if (vec[i] == val) count++; } return count;}
vector<int> v; count_occurrences(v, 5);
🤔 What is an assumption we’re making here? (Type in the chat.)
![Page 59: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/59.jpg)
How many times does an int appear in a vector of ints?
int count_occurrences(const vector<int>& vec, int val) { int count = 0; for (size_t i = 0; i < vec.size(); i++) { if (vec[i] == val) count++; } return count;}
vector<int> v; count_occurrences(v, 5);
🤔 What if we want to generalize this beyond ints?
![Page 60: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/60.jpg)
How many times does a <T> appear in a vector<T>?
template <typename DataType>int count_occurrences(const vector<DataType>& vec, DataType val) { int count = 0; for (size_t i = 0; i < vec.size(); i++) { if (vec[i] == val) count++; } return count;}
vector<string> v; count_occurrences(v, “test”);
🤔 Perfect! But what if we want to generalize this beyond a vector?
![Page 61: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/61.jpg)
One possibility...
template <typename Collection, typename DataType>int count_occurrences(const Collection& arr, DataType val) { int count = 0; for (size_t i = 0; i < arr.size(); i++) { if (arr[i] == val) count++; } return count;}
vector<string> v; count_occurrences(v, “test”);
🤔 What is wrong with this? (Type in the chat.)🚫 The collection may not be indexable. How can we solve this?
![Page 62: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/62.jpg)
How many times does a <T> appear in an iterator<T>?
template <typename InputIt, typename DataType>int count_occurrences(InputIt begin, InputIt end, DataType val) { int count = 0; for (initialization; end-condition; increment) { if (retrieval == val) count++; } return count;}
vector<string> v; count_occurrences(arg1, arg2, “test”);
🤔 Practice by filling in the blanks in the chat!
![Page 63: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/63.jpg)
How many times does a <T> appear in an iterator<T>?
template <typename InputIt, typename DataType>int count_occurrences(InputIt begin, InputIt end, DataType val) { int count = 0; for (auto iter = begin; iter != end; ++iter) { if (*iter == val) count++; } return count;}
vector<string> v; count_occurrences(v.begin(), v.end(), “test”);
🤔 Great! 📝 We manually pass in begin and end so that we can customize our search bounds.
![Page 64: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/64.jpg)
Live Code Demo:Count Occurrences
![Page 65: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/65.jpg)
We can now solve these questions...Count the number of times 3 appears in a list<int>.Count the number of times ‘X’ appears in a std::deque<char>.Count the number of times ‘Y’ appears in a string.Count the number of times 5 appears in the second half of a vector<int>.
But how about this?
Count the number of times an odd number appears in a vector<int>.Count the number of times a vowel appears in a string.
![Page 66: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/66.jpg)
🤔 Questions? 🤔
![Page 67: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/67.jpg)
Recap
● Template functions○ lets you declare functions that can accept different types as parameters!
● (Supplemental) Variadic templates○ lets you declare functions that can accept an arbitrary number of
parameters!● Concept lifting
○ technique that we use to see how to generalize our code!
![Page 68: Lecture 7: Template Functionstemplate Specifies T is some arbitrary type List of template arguments Note: Scope of template argument T is limited to this one function! Just in case](https://reader034.fdocuments.in/reader034/viewer/2022051815/603b818e3b5b4608193161bd/html5/thumbnails/68.jpg)
Next time:lambda functions and algorithms