![Page 1: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/1.jpg)
A Crash Course in(Some of) Modern C++
CMPT 373Software Development Methods
Nick [email protected]
With material from Bjarne Stroustrup & Herb Sutter
![Page 2: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/2.jpg)
C++ was complicated/intimidating
● Pointers– Arithmetic & indexing– dangling– when to new and delete
![Page 3: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/3.jpg)
C++ was complicated/intimidating
● Pointers– Arithmetic & indexing– dangling– when to new and delete
● Nontrivial types– inheritance– long names & scoping (iterators)– templates
![Page 4: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/4.jpg)
C++ was complicated/intimidating
● Pointers– Arithmetic & indexing– dangling– when to new and delete
● Nontrivial types– inheritance– long names & scoping (iterators)– templates
● Many proposed rules (of varying validity)– Rule of 3– Don’t pass/return objects to/from functions by value– ...
![Page 5: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/5.jpg)
Modern C++
● Significant effort has gone into revising C++ since C++03– Identifying & simplifying unnecessary complexity
– Adopting features that help reduce complexity in large scale projects.
![Page 6: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/6.jpg)
Modern C++
● Significant effort has gone into revising C++ since C++03– Identifying & simplifying unnecessary complexity
– Adopting features that help reduce complexity in large scale projects.
● Safety– types, bounds, lifetimes
![Page 7: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/7.jpg)
Modern C++
● Significant effort has gone into revising C++ since C++03– Identifying & simplifying unnecessary complexity
– Adopting features that help reduce complexity in large scale projects.
● Safety– types, bounds, lifetimes
● Syntactic sugar (with safety benefits)
![Page 8: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/8.jpg)
Modern C++
● Significant effort has gone into revising C++ since C++03– Identifying & simplifying unnecessary complexity
– Adopting features that help reduce complexity in large scale projects.
● Safety– types, bounds, lifetimes
● Syntactic sugar (with safety benefits)
● Now developed under a lightweight process with new revisions every ~3 years.
![Page 9: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/9.jpg)
Modern C++
● Significant effort has gone into revising C++ since C++03– Identifying & simplifying unnecessary complexity
– Adopting features that help reduce complexity in large scale projects.
● Safety– types, bounds, lifetimes
● Syntactic sugar (with safety benefits)
● Now developed under a lightweight process with new revisions every ~3 years.
To get you (re)acquainted,we will explore some of modern C++ for now.
I will assume familiarity with older C++,constructors, destructors, etc.
![Page 10: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/10.jpg)
Managing Object Lifetimes
Suppose I have a Widget class constructed from an int and a string.
![Page 11: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/11.jpg)
Managing Object Lifetimes
Suppose I have a Widget class constructed from an int and a string.
● How might I create one?
![Page 12: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/12.jpg)
Managing Object Lifetimes
Suppose I have a Widget class constructed from an int and a string.
● How might I create one?
Widget w{0, "fritter"};
![Page 13: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/13.jpg)
Managing Object Lifetimes
Suppose I have a Widget class constructed from an int and a string.
● How might I create one?Brace initialization is new in C++11
Widget w{0, "fritter"};
![Page 14: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/14.jpg)
Managing Object Lifetimes
Suppose I have a Widget class constructed from an int and a string.
● How might I create one?
Where does w live in memory?Is that good/bad?
Widget w{0, "fritter"};
![Page 15: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/15.jpg)
Managing Object Lifetimes
Suppose I have a Widget class constructed from an int and a string.
● How might I create one?
– Automatic variables/management should be the default.
Widget w{0, "fritter"};
![Page 16: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/16.jpg)
Managing Object Lifetimes
Suppose I have a Widget class constructed from an int and a string.
● How might I create one?
– Automatic variables/management should be the default.
● What about creating one on the heap?
Widget w{0, "fritter"};
![Page 17: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/17.jpg)
Managing Object Lifetimes
Suppose I have a Widget class constructed from an int and a string.
● How might I create one?
– Automatic variables/management should be the default.
● What about creating one on the heap?
Widget* w = new Widget{0, "fritter"};Old:
Widget w{0, "fritter"};
![Page 18: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/18.jpg)
Managing Object Lifetimes
Suppose I have a Widget class constructed from an int and a string.
● How might I create one?
– Automatic variables/management should be the default.
● What about creating one on the heap?
Old:
Widget w{0, "fritter"};
Widget* w = new Widget{0, "fritter"};
What problems does this create?
![Page 19: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/19.jpg)
Managing Object Lifetimes
Suppose I have a Widget class constructed from an int and a string.
● How might I create one?
– Automatic variables/management should be the default.
● What about creating one on the heap?
– Need to delete everything.– Need to delete everything only once.– Complex object graphs make this harder
Old:
Widget w{0, "fritter"};
Widget* w = new Widget{0, "fritter"};
![Page 20: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/20.jpg)
Managing Object Lifetimes
Object graphs/lifetimes are complex
Stack
![Page 21: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/21.jpg)
Managing Object Lifetimes
Object graphs/lifetimes are complex
StackWhich pointers can I delete & when?
![Page 22: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/22.jpg)
Managing Object Lifetimes
Object graphs/lifetimes are complex
StackWhich pointers can I delete & when?
![Page 23: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/23.jpg)
Managing Object Lifetimes
Object graphs/lifetimes are complex
StackWhich pointers can I delete & when?
![Page 24: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/24.jpg)
Managing Object Lifetimes
Object graphs/lifetimes are complex
StackWhich pointers can I delete & when?A
B
![Page 25: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/25.jpg)
Managing Object Lifetimes
Object graphs/lifetimes are complex
StackWhich pointers can I delete & when?
![Page 26: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/26.jpg)
Managing Object Lifetimes
Object graphs/lifetimes are complex
StackWhich pointers can I delete & when?
![Page 27: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/27.jpg)
Managing Object Lifetimes
Object graphs/lifetimes are complex
● Could this problem be solved using only std::vector?
Stack
![Page 28: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/28.jpg)
Managing Object Lifetimes
Object graphs/lifetimes are complex
● Could this problem be solved using only std::vector?
Stack
Roughly...
![Page 29: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/29.jpg)
Managing Object Lifetimes
Object graphs/lifetimes are complex
● Could this problem be solved using only std::vector?
Stackroot
extra
a
c d
b
struct Node { std::vector<Node> owned; Node* raw1, raw2; Node() : raw1(NULL), raw2(NULL) {}};
Node root, extra;root.resize(2); // create a,broot.owned[0].resize(1) // create croot.owned[1].resize(1) // create droot.owned[0].raw1 = &root.owned[1];root.owned[1].raw1 = &root.owned[0];root.owned[0].raw2 = &extra;
I am not advocating it,but memory management is automatic
![Page 30: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/30.jpg)
Managing Object Lifetimes
Object graphs/lifetimes are complex
● Could this problem be solved using only std::vector?
● Are there any downsides to doing so?
Stack
![Page 31: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/31.jpg)
Managing Object Lifetimes
Object graphs/lifetimes are complex
● Could this problem be solved using only std::vector?
● Are there any downsides to doing so?– Unclear?– Unnecessary overheads?– Mismatched lifetimes?
Stack
![Page 32: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/32.jpg)
Managing Object Lifetimes
Object graphs/lifetimes are complex
● Could this problem be solved using only std::vector?
● Are there any downsides to doing so?– Unclear?– Unnecessary overheads?– Mismatched lifetimes?
Stack
What we want is a clear, intentional way to express ownership.
![Page 33: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/33.jpg)
Managing Object Lifetimes
● 2 types of ownership in modern C++
![Page 34: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/34.jpg)
Managing Object Lifetimes
● 2 types of ownership in modern C++– Unique ownership (std::unique_ptr<T>)
auto w = std::make_unique<Widget>(0, "cruller");
![Page 35: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/35.jpg)
Managing Object Lifetimes
● 2 types of ownership in modern C++– Unique ownership (std::unique_ptr<T>)
● deletes the object when w goes out of scope
● Automated (even with exceptions)
auto w = std::make_unique<Widget>(0, "cruller");
![Page 36: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/36.jpg)
Managing Object Lifetimes
● 2 types of ownership in modern C++– Unique ownership (std::unique_ptr<T>)
● deletes the object when w goes out of scope
● Automated (even with exceptions)● Generally preferred
auto w = std::make_unique<Widget>(0, "cruller");
![Page 37: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/37.jpg)
Managing Object Lifetimes
● 2 types of ownership in modern C++– Unique ownership (std::unique_ptr<T>)
● deletes the object when w goes out of scope
● Automated (even with exceptions)● Generally preferred
– Shared ownership (std::shared_ptr<T>)
auto w = std::make_unique<Widget>(0, "cruller");
auto w = std::make_shared<Widget>(0, "ponchik");
![Page 38: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/38.jpg)
Managing Object Lifetimes
● 2 types of ownership in modern C++– Unique ownership (std::unique_ptr<T>)
● deletes the object when w goes out of scope
● Automated (even with exceptions)● Generally preferred
– Shared ownership (std::shared_ptr<T>)
● Counts the number of owners
● deletes the object when # owners --> 0
auto w = std::make_unique<Widget>(0, "cruller");
auto w = std::make_shared<Widget>(0, "ponchik");
![Page 39: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/39.jpg)
Managing Object Lifetimes
● 2 types of ownership in modern C++– Unique ownership (std::unique_ptr<T>)
● deletes the object when w goes out of scope
● Automated (even with exceptions)● Generally preferred
– Shared ownership (std::shared_ptr<T>)
● Counts the number of owners
● deletes the object when # owners --> 0
auto w = std::make_unique<Widget>(0, "cruller");
auto w = std::make_shared<Widget>(0, "ponchik");
What happens if you have a cycle?
![Page 40: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/40.jpg)
Managing Object Lifetimes
● 2 types of ownership in modern C++– Unique ownership (std::unique_ptr<T>)
● deletes the object when w goes out of scope
● Automated (even with exceptions)● Generally preferred
– Shared ownership (std::shared_ptr<T>)
● Counts the number of owners
● deletes the object when # owners --> 0
● Ownership can also be transferred
auto w = std::make_unique<Widget>(0, "cruller");
auto w = std::make_shared<Widget>(0, "ponchik");
![Page 41: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/41.jpg)
Managing Object Lifetimes
● A few rules:– Every object has (preferably) one owner
Stack
![Page 42: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/42.jpg)
Managing Object Lifetimes
● A few rules:– Every object has (preferably) one owner
Stack
?
![Page 43: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/43.jpg)
Managing Object Lifetimes
● A few rules:– Every object has (preferably) one owner
– No object outlives the scope of its owning pointer
Stack
![Page 44: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/44.jpg)
Managing Object Lifetimes
● A few rules:– Every object has (preferably) one owner
– No object outlives the scope of its owning pointer
– Non-owning pointers/references can be unlimited● But should not outlive the owning scope by design
Stack
![Page 45: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/45.jpg)
Managing Object Lifetimes
● A few rules:– Every object has (preferably) one owner
– No object outlives the scope of its owning pointer
– Non-owning pointers/references can be unlimited● But should not outlive the owning scope by design
Stack
Note: Unique owning pointers form a spanning tree within the heap.
![Page 46: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/46.jpg)
Functions (a slight digression)
What is the signature to...
● pass an argument of class type X to a function?
![Page 47: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/47.jpg)
Functions (a slight digression)
What is the signature to...
● pass an argument of class type X to a function?foo(const X&)
![Page 48: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/48.jpg)
Functions (a slight digression)
What is the signature to...
● pass an argument of class type X to a function?
● pass a mutable argument of class type X to a function?
foo(const X&)
![Page 49: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/49.jpg)
Functions (a slight digression)
What is the signature to...
● pass an argument of class type X to a function?
● pass a mutable argument of class type X to a function?
foo(const X&)
foo(X&)
![Page 50: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/50.jpg)
Functions (a slight digression)
What is the signature to...
● pass an argument of class type X to a function?
● pass a mutable argument of class type X to a function?
● pass an instance of X to a function making a copy?
foo(const X&)
foo(X&)
![Page 51: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/51.jpg)
Functions (a slight digression)
What is the signature to...
● pass an argument of class type X to a function?
● pass a mutable argument of class type X to a function?
● pass an instance of X to a function making a copy?
foo(const X&)
foo(X&)
foo(X)
![Page 52: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/52.jpg)
Using What You Know
● What should go in 1 and 2 to pass w to foo?
– (It may depend on what you want to do...)– Do you just want to give foo access to the Widget?– Do you want foo to modify the ownership?– Do you want to transfer ownership to foo?
void foo( );
void bar() { auto w = std::make_unique<Widget>(42, "churro"); foo( );}
1
2
![Page 53: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/53.jpg)
Using What You Know
● What should go in 1 and 2 to pass w to foo?
– (It may depend on what you want to do...)– Do you just want to give foo access to the Widget?– Do you want foo to modify the ownership?– Do you want to transfer ownership to foo?
void foo( );
void bar() { auto w = std::make_unique<Widget>(42, "churro"); foo( );}
1
2
Note: These are behaviors that would already happen.Smart pointers make them explicit and automatic.
![Page 54: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/54.jpg)
General Resource Management
● Memory management is just one example of resource management.
![Page 55: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/55.jpg)
General Resource Management
● Memory management is just one example of resource management.– Properly acquiring & releasing resources
![Page 56: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/56.jpg)
General Resource Management
● Memory management is just one example of resource management.– Properly acquiring & releasing resources
● No double acquisition.● No double free.● No use after free.● No leaks
![Page 57: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/57.jpg)
General Resource Management
● Memory management is just one example of resource management.– Properly acquiring & releasing resources
● No double acquisition.● No double free.● No use after free.● No leaks
– What other resources do you manage?
![Page 58: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/58.jpg)
General Resource Management
● Memory management is just one example of resource management.– Properly acquiring & releasing resources
● No double acquisition.● No double free.● No use after free.● No leaks
– What other resources do you manage?● Files● Locks● Database connections● Printers● ...
![Page 59: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/59.jpg)
General Resource Management
● The problem is pervasive enough to have general solutions
![Page 60: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/60.jpg)
General Resource Management
● The problem is pervasive enough to have general solutions– Python: ?
![Page 61: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/61.jpg)
General Resource Management
● The problem is pervasive enough to have general solutions– Python: with
![Page 62: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/62.jpg)
General Resource Management
● The problem is pervasive enough to have general solutions– Python: with– C#: using– Java: try-with-resources
![Page 63: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/63.jpg)
General Resource Management
● The problem is pervasive enough to have general solutions– Python: with– C#: using– Java: try-with-resources– C++: ?
![Page 64: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/64.jpg)
General Resource Management
● The problem is pervasive enough to have general solutions– Python: with– C#: using– Java: try-with-resources– C++: RAII (Resource Acquisition is Initialization)
![Page 65: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/65.jpg)
General Resource Management
● The problem is pervasive enough to have general solutions– Python: with– C#: using– Java: try-with-resources– C++: RAII (Resource Acquisition is Initialization)
● Goal: Simplify & control the lifetimes of resources
![Page 66: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/66.jpg)
General Resource Management
● The problem is pervasive enough to have general solutions– Python: with– C#: using– Java: try-with-resources– C++: RAII (Resource Acquisition is Initialization)
● Goal: Simplify & control the lifetimes of resources
● RAII– Bind the lifetime of the resource to object lifetime
![Page 67: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/67.jpg)
General Resource Management
● The problem is pervasive enough to have general solutions– Python: with– C#: using– Java: try-with-resources– C++: RAII (Resource Acquisition is Initialization)
● Goal: Simplify & control the lifetimes of resources
● RAII– Bind the lifetime of the resource to object lifetime– Acquire the resource in the constructor
![Page 68: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/68.jpg)
General Resource Management
● The problem is pervasive enough to have general solutions– Python: with– C#: using– Java: try-with-resources– C++: RAII (Resource Acquisition is Initialization)
● Goal: Simplify & control the lifetimes of resources
● RAII– Bind the lifetime of the resource to object lifetime– Acquire the resource in the constructor– Release the resource in the destructor
![Page 69: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/69.jpg)
General Resource Management
● Memory
void memoryResource() { auto w = std::make_unique<Widget>(3, "bofrot"); foo(*w);}
![Page 70: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/70.jpg)
void memoryResource() { auto w = std::make_unique<Widget>(3, "bofrot"); foo(*w);}
General Resource Management
● Memory
w is automatically deallocated here.
![Page 71: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/71.jpg)
General Resource Management
● Memory
● Filesvoid fileResource() { auto out = std::ofstream{"output.txt"}; out << "Boston cream\n";}
void memoryResource() { auto w = std::make_unique<Widget>(3, "bofrot"); foo(*w);} w is automatically deallocated here.
![Page 72: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/72.jpg)
General Resource Management
● Memory
● Files
void memoryResource() { auto w = std::make_unique<Widget>(3, "bofrot"); foo(*w);} w is automatically deallocated here.
void fileResource() { auto out = std::ofstream{"output.txt"}; out << "Boston cream\n";} out is automatically flushed & closed here.
![Page 73: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/73.jpg)
General Resource Management
● Memory
● Files
● Because they are scoped, they handle exceptions & multiple return statements!
void memoryResource() { auto w = std::make_unique<Widget>(3, "bofrot"); foo(*w);} w is automatically deallocated here.
void fileResource() { auto out = std::ofstream{"output.txt"}; out << "Boston cream\n";} out is automatically flushed & closed here.
![Page 74: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/74.jpg)
General Resource Management
● How does RAII relate to managing complexity?
![Page 75: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/75.jpg)
General Resource Management
● How does RAII relate to managing complexity?– It makes resource designs explicit– It makes managing them automatic– It removes temporal coupling– It promotes composition & independence
![Page 76: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/76.jpg)
General Resource Management
● How does RAII relate to managing complexity?– It makes resource designs explicit– It makes managing them automatic– It removes temporal coupling– It promotes composition & independence
● NOTE: What happens when you copy a resource object?
![Page 77: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/77.jpg)
General Resource Management
● How does RAII relate to managing complexity?– It makes resource designs explicit– It makes managing them automatic– It removes temporal coupling– It promotes composition & independence
● NOTE: What happens when you copy a resource object?– In many cases, it is explicitly forbidden
Why?
![Page 78: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/78.jpg)
General Resource Management
● How does RAII relate to managing complexity?– It makes resource designs explicit– It makes managing them automatic– It removes temporal coupling– It promotes composition & independence
● NOTE: What happens when you copy a resource object?– In many cases, it is explicitly forbidden– You can use std::move() to transfer resource ownership
![Page 79: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/79.jpg)
Operating on Collections
● Iterating over collections can be painful
void oops() { std::vector<int> numbers = {0, 1, 2, 3, 4}; for (unsigned i = 0, e = 4; i <= 4; ++i) { std::cout << numbers[i] << "\n"; }}
![Page 80: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/80.jpg)
Operating on Collections
● Iterating over collections can be painful
● Range based for loops are preferable
void nice() { std::vector<int> numbers = {0, 1, 2, 3, 4}; for (auto number : numbers) { std::cout << number << "\n"; }}
void oops() { std::vector<int> numbers = {0, 1, 2, 3, 4}; for (unsigned i = 0, e = 4; i <= 4; ++i) { std::cout << numbers[i] << "\n"; }}
![Page 81: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/81.jpg)
Operating on Collections
● Iterating over collections can be painful
● Range based for loops are preferable
void nice() { std::vector<int> numbers = {0, 1, 2, 3, 4}; for (auto number : numbers) { std::cout << number << "\n"; }}
void oops() { std::vector<int> numbers = {0, 1, 2, 3, 4}; for (unsigned i = 0, e = 4; i <= 4; ++i) { std::cout << numbers[i] << "\n"; }}
The “collection” can be anything with begin() and end() methods.
![Page 82: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/82.jpg)
Operating on Collections
● Passing collections around can be error prone.
void oops(const std::vector<int> numbers) { ...}
![Page 83: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/83.jpg)
Operating on Collections
● Passing collections around can be error prone.
● Avoid unnecessary copies.
void oops(const std::vector<int> numbers) { ...}
void better(const std::vector<int>& numbers) { ...}
![Page 84: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/84.jpg)
Operating on Collections
● Passing collections around can be error prone.
● Avoid unnecessary copies.
● Use std::span in C++20 for flexibility & correctness by design
void oops(const std::vector<int> numbers) { ...}
void better(const std::vector<int>& numbers) { ...}
void good(const std::span<int> numbers) { ...}
![Page 85: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/85.jpg)
Guideline Support Library
Some common classes for better code, specifically:
![Page 86: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/86.jpg)
Guideline Support Library
Some common classes for better code, specifically:
● gsl::span<T>– Makes interfaces generic & safer if you do not have C++20
[demo]
![Page 87: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/87.jpg)
Guideline Support Library
Some common classes for better code, specifically:
● gsl::span<T>– Makes interfaces generic & safer if you do not have c++20
[demo]
● std::string_view<T>– Avoid copying strings
– Avoid conversions to and from C strings(a common mistake!)
![Page 88: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/88.jpg)
Guideline Support Library
Some common classes for better code, specifically:
● gsl::span<T>– Makes interfaces generic & safer
[demo]
● std::string_view<T>– Avoid copying strings
– Avoid conversions to and from C strings(a common mistake!)
● Both of these abstractions are non-owning
![Page 89: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/89.jpg)
λ (Lambdas)
● How should you check whether a list contains a numbergreater than 3?
![Page 90: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/90.jpg)
λ (Lambdas)
● How should you check whether a list contains a numbergreater than 3?
bool hasGreaterThan3 = false;for (auto number : numbers) { if (number > 3) { hasGreaterThan3 = true; }}
![Page 91: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/91.jpg)
λ (Lambdas)
● How should you check whether a list contains a numbergreater than 3?
bool hasGreaterThan3 = false;for (auto number : numbers) { if (number > 3) { hasGreaterThan3 = true; }} Using a general purpose loop
hides the high level intentions.
![Page 92: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/92.jpg)
λ (Lambdas)
● How should you check whether a list contains a numbergreater than 3?
bool hasGreaterThan3 = false;for (auto number : numbers) { if (number > 3) { hasGreaterThan3 = true; }} Using a general purpose loop
hides the high level intentions.bool hasGreaterThan3 = std::any_of(numbers.begin(), numbers.end(), [](auto number) { return number > 3; });
bool hasGreaterThan3 = std::any_of(numbers, [](auto number) { return number > 3; });In C++20:
![Page 93: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/93.jpg)
λ (Lambdas)
● Lambdas allow you to create small, self contained functions local to other code
[local1, &local2](auto arg1, auto arg2) { ...}
![Page 94: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/94.jpg)
λ (Lambdas)
● Lambdas allow you to create small, self contained functions local to other code
[local1, &local2](auto arg1, auto arg2) { ...}
You can capture arguments from the local scope.
![Page 95: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/95.jpg)
λ (Lambdas)
● Lambdas allow you to create small, self contained functions local to other code
[local1, &local2](auto arg1, auto arg2) { ...}
You can capture arguments from the local scope.
Note: While I am showing you that you can capture by reference, you should not.
![Page 96: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/96.jpg)
λ (Lambdas)
● Lambdas allow you to create small, self contained functions local to other code
[local1, &local2](auto arg1, auto arg2) { ...}
Additional arguments are passed in when invoked.
![Page 97: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/97.jpg)
λ (Lambdas)
● Lambdas allow you to create small, self contained functions local to other code
● Lambdas allow you to use generic library functions in a clear, well localized fashion.
[local1, &local2](auto arg1, auto arg2) { ...}
![Page 98: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/98.jpg)
λ (Lambdas)
● Lambdas allow you to create small, self contained functions local to other code
● Lambdas allow you to use generic library functions in a clear, well localized fashion.
[local1, &local2](auto arg1, auto arg2) { ...}
auto found = std::find_if(numbers.begin(), numbers.end(), [](auto number) { return number > 3; });std::cout << *found << " is greater than 3.\n";
![Page 99: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/99.jpg)
λ (Lambdas)
● Lambdas allow you to create small, self contained functions local to other code
● Lambdas allow you to use generic library functions in a clear, well localized fashion.
[local1, &local2](auto arg1, auto arg2) { ...}
auto found = std::find_if(numbers.begin(), numbers.end(), [](auto number) { return number > 3; });std::cout << *found << " is greater than 3.\n";See <algorithm>
![Page 100: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/100.jpg)
λ (Lambdas)
● Lambdas allow you to create small, self contained functions local to other code
● Lambdas allow you to use generic library functions in a clear, well localized fashion.
[local1, &local2](auto arg1, auto arg2) { ...}
auto found = std::find_if(numbers.begin(), numbers.end(), [](auto number) { return number > 3; });std::cout << *found << " is greater than 3.\n";See <algorithm>
I will expect you to make use of built in algorithms and lambdas instead of raw loops from now on.
![Page 101: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/101.jpg)
Exceptions
● Not new, but maybe new to you in C++
![Page 102: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/102.jpg)
Exceptions
● Not new, but maybe new to you in C++
● Can use existing exception types <stdexcept>
![Page 103: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/103.jpg)
Exceptions
● Not new, but maybe new to you in C++
● Can use existing exception types <stdexcept>
try { throw std::runtime_error("uh oh...");} catch (const std::runtime_error& e) { std::cout << "Exception message: " << e.what();}
![Page 104: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/104.jpg)
Exceptions
● Not new, but maybe new to you in C++
● Can use existing exception types <stdexcept>
try { throw std::runtime_error("uh oh...");} catch (const std::runtime_error& e) { std::cout << "Exception message: " << e.what();}
Throw by value.
![Page 105: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/105.jpg)
Exceptions
● Not new, but maybe new to you in C++
● Can use existing exception types <stdexcept>
try { throw std::runtime_error("uh oh...");} catch (const std::runtime_error& e) { std::cout << "Exception message: " << e.what();}
Catch by reference.
![Page 106: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/106.jpg)
Exceptions
● Not new, but maybe new to you in C++
● Can use existing exception types <stdexcept>
try { throw std::runtime_error("uh oh...");} catch (const std::runtime_error& e) { std::cout << "Exception message: " << e.what();}
Error messages.
![Page 107: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/107.jpg)
Exceptions
● Not new, but maybe new to you in C++
● Can use existing exception types <stdexcept>● Or you can create custom exceptions
![Page 108: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/108.jpg)
Exceptions
● Not new, but maybe new to you in C++
● Can use existing exception types <stdexcept>● Or you can create custom exceptions
class MyException : public std::runtime_error {public: const char * what() const override { ... }};
![Page 109: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/109.jpg)
Exceptions
● Not new, but maybe new to you in C++
● Can use existing exception types <stdexcept>● Or you can create custom exceptions
class MyException : public std::runtime_error {public: const char * what() const override { ... }};
![Page 110: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/110.jpg)
Exceptions
● Not new, but maybe new to you in C++
● Can use existing exception types <stdexcept>● Or you can create custom exceptions
class MyException : public std::runtime_error {public: const char * what() const override { ... }};
![Page 111: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/111.jpg)
More...
● std::array<T,N>
![Page 112: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/112.jpg)
More...
● std::array<T,N>● nullptr
![Page 113: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/113.jpg)
More...
● std::array<T,N>● nullptr● auto (even for return & lambda arg types)
![Page 114: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/114.jpg)
More...
● std::array<T,N>● nullptr● auto (even for return & lambda arg types)
● constexpr● type safe enums
● delegating constructors
● using instead of typedef
![Page 115: CMPT 373 Software Development Methods A Crash Course in ...wsumner/teaching/373/04-modernc++.pdf · Modern C++ Significant effort has gone into revising C++ since C++03 – Identifying](https://reader036.vdocuments.net/reader036/viewer/2022071210/6020b70601603554fd093d28/html5/thumbnails/115.jpg)
More...
● std::array<T,N>● nullptr● auto (even for return & lambda arg types)
● constexpr● type safe enums
● delegating constructors
● using instead of typedef
● Destructuring: auto [x, y] = std::make_pair(3,4);● ...