Understanding and Mastering C++'s Complexity
Amir Kirsh
©
All rights reserved
RAII
IILE
ODR
ADL
RVO
IFNDR
UB
CTAD
CRTP
CWG
LTO
EBO
RTTI
SFINAE
TU
UDL
Before we .begin()
2
Understanding and mastering C++'s complexity @ CppCon 2021
Before we .begin()
Does the code below compile? If so, how? If not, why?
(Wait… question is open only for C++ programmers with 0-3 years of experience)
3
Understanding and mastering C++'s complexity @ CppCon 2021
Before we .begin()
Does the code below compile? If so, how? If not, why?
#include <utility>
#include <string>
int main() {
std::string s1 = "hi", s2 = "bye";
swap(s1, s2);
int a = 3, b = 7;
swap(a, b);
}
4
Understanding and mastering C++'s complexity @ CppCon 2021
Complexity
What this talk is about (and what’s not)
5
Understanding and mastering C++'s complexity @ CppCon 2021
Complexity
This talk is NOT about Algorithmic Complexity (no big ‘O’ in this talk!)
6
Understanding and mastering C++'s complexity @ CppCon 2021
Complexity
This talk is NOT about Algorithmic Complexity (no big ‘O’ in this talk!)
But we do have a talk on�Algorithmic Complexity - on Friday!
7
Understanding and mastering C++'s complexity @ CppCon 2021
Complexity
This talk is about C++ language complexity,�with a broad definition for complexity:
anything that makes it hard for you to use C++, or to understand it,�including things that irritate or annoy you, things that waste your time,�and language syntax that is bug prone, or broken in a way, or is done easier�in other languages.
8
Understanding and mastering C++'s complexity @ CppCon 2021
About Me
9
Understanding and mastering C++'s complexity @ CppCon 2021
Amir Kirsh
10
Understanding and mastering C++'s complexity @ CppCon 2021
Lecturer�Academic College of Tel-Aviv-Yaffo
and Tel-Aviv University
Developer Advocate
Co-Organizer of the CoreCpp�conference and meetup group
Talk Origins
A graduated student of mine was interviewed for a C++ position and consulted me whether C++ is a right choice (as “there are other less complex languages”).
11
Understanding and mastering C++'s complexity @ CppCon 2021
Talk Origins
A graduated student of mine was interviewed for a C++ position and consulted me whether C++ is a right choice (as “there are other less complex languages”).
I convinced her.
12
Understanding and mastering C++'s complexity @ CppCon 2021
Talk Origins
A graduated student of mine was interviewed for a C++ position and consulted me whether C++ is a right choice (as “there are other less complex languages”).
I convinced her.
She is now a C++ developer at Waves.com
13
Understanding and mastering C++'s complexity @ CppCon 2021
Amit Barzilay
Talk Origins
A graduated student of mine was interviewed for a C++ position and consulted me whether C++ is a right choice (as “there are other less complex languages”).
I convinced her.
She is now a C++ developer at Waves.com
We prepared this talk together for CoreCpp conference 2021 in Tel-Aviv.
And now you got me here :-)
14
Understanding and mastering C++'s complexity @ CppCon 2021
Amit Barzilay
Complexity
Isn’t it the name of the game? (of being a programmer...)
15
Understanding and mastering C++'s complexity @ CppCon 2021
The Perils of Java Schools
16
Understanding and mastering C++'s complexity @ CppCon 2021
Joel Spolsky, 2005
Lazy kids.
Whatever happened to hard work?
...
in the last decade a large number of otherwise perfectly good schools have gone 100% Java ...
The lucky kids of JavaSchools are never going to get weird segfaults trying to implement pointer-based hash tables. They’re never going to go stark, raving mad trying to pack things into bits.
https://www.joelonsoftware.com/2005/12/29/the-perils-of-javaschools-2
Complexity
Is C++ complex?
Why?
Can it be less complex?
17
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex - for you?
18
Understanding and mastering C++'s complexity @ CppCon 2021
Please check only the things that bother you in person, don’t check topics that you are not familiar with or don’t use at all:
What makes C++ complex - for you?
19
Understanding and mastering C++'s complexity @ CppCon 2021
Please check only the things that bother you in person, don’t check topics that you are not familiar with or don’t use at all:
What makes C++ complex - for you?
20
Understanding and mastering C++'s complexity @ CppCon 2021
Let’s review the questionnaire results
Complexity
21
Understanding and mastering C++'s complexity @ CppCon 2021
Complexity
Why programming is complex?
22
Understanding and mastering C++'s complexity @ CppCon 2021
What makes a software language complex?
23
Understanding and mastering C++'s complexity @ CppCon 2021
What makes a software language complex?
24
Understanding and mastering C++'s complexity @ CppCon 2021
What makes a software language complex?
25
Understanding and mastering C++'s complexity @ CppCon 2021
What makes a software language complex?
26
Understanding and mastering C++'s complexity @ CppCon 2021
What makes a software language complex?
27
Understanding and mastering C++'s complexity @ CppCon 2021
What makes a software language complex?
28
Understanding and mastering C++'s complexity @ CppCon 2021
What makes a software language complex?
29
Understanding and mastering C++'s complexity @ CppCon 2021
What makes a software language complex?
30
Understanding and mastering C++'s complexity @ CppCon 2021
What makes a software language complex?
31
Understanding and mastering C++'s complexity @ CppCon 2021
What makes a software language complex?
32
Understanding and mastering C++'s complexity @ CppCon 2021
Complexity
Is C++ complex?
Why?
Can it be less complex?
33
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
34
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
35
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
36
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
37
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
38
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
39
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
40
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
41
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
42
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
43
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
44
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
45
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
46
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
47
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
48
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
49
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
50
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
51
Understanding and mastering C++'s complexity @ CppCon 2021
What makes C++ complex?
52
Understanding and mastering C++'s complexity @ CppCon 2021
and that’s what you hold against a language with >5M users and billions lines of code??
There is no silver bullet
Picture: https://www.infoq.com/articles/No-Silver-Bullet-Summary -- OOPSLA 2005, Montreal
“No Silver Bullet - Essence and Accident in Software Engineering” by Fred Brooks, 1986
53
Essence or Accident?
54
Understanding and mastering C++'s complexity @ CppCon 2021
Essence or Accident?
55
Understanding and mastering C++'s complexity @ CppCon 2021
Let’s play...
Essence or Accident?
56
Understanding and mastering C++'s complexity @ CppCon 2021
std::map<std::string, std::list<std::pair<Date, Price>>>::iterator quotesItr
= stocks.find(id);
Essence or Accident?
57
Understanding and mastering C++'s complexity @ CppCon 2021
std::map<std::string, std::list<std::pair<Date, Price>>>::iterator quotesItr
= stocks.find(id);
auto quotesItr = stocks.find(id);
C++11
Essence or Accident?
58
Understanding and mastering C++'s complexity @ CppCon 2021
Essence or Accident?
59
Understanding and mastering C++'s complexity @ CppCon 2021
What’s the problem here:
class Shape {
Color color;
public:
virtual void draw() const = 0;
virtual void move(int diffX, int diffY) = 0;
};
Can the compiler deduce that a class *needs*�a virtual destructor and provide one?
60
Understanding and mastering C++'s complexity @ CppCon 2021
Can the compiler deduce that a class *needs*�a virtual destructor and provide one?
61
Understanding and mastering C++'s complexity @ CppCon 2021
What are the actual rules for “you must have a virtual destructor”?
Can the compiler deduce that a class *needs*�a virtual destructor and provide one?
62
Understanding and mastering C++'s complexity @ CppCon 2021
What are the actual rules for “you must have a virtual destructor”?
int main() {
Rect r ({10, 10}, {20, 20});
Shape* p = &r;
p->draw();
}
class Shape {
Color color;
public:
virtual void draw() const = 0;
virtual void move(int diffX, int diffY) = 0;
};
Essence or Accident?
63
Understanding and mastering C++'s complexity @ CppCon 2021
Essence or Accident?
64
Understanding and mastering C++'s complexity @ CppCon 2021
template<class K, class V, size_t SIZE, class FetchFunc>
class Cache {
struct Holder {
V val;
mutable typename list<K>::iterator posInList;
Holder(V v, Date exp, typename list<K>::iterator pos)
: val(v), posInList(pos), expiry(exp), accessed(Date()) {}
// ...
private:
Date expiry;
mutable Date accessed;
};
// ...
};
Essence or Accident? C++20
65
Understanding and mastering C++'s complexity @ CppCon 2021
template<class K, class V, size_t SIZE, class FetchFunc>
class Cache {
struct Holder {
V val;
mutable typename list<K>::iterator posInList;
Holder(V v, Date exp, typename list<K>::iterator pos)
: val(v), posInList(pos), expiry(exp), accessed(Date()) {}
// ...
private:
Date expiry;
mutable Date accessed;
};
// ...
};
Essence or Accident?
66
Understanding and mastering C++'s complexity @ CppCon 2021
Essence or Accident?
67
Understanding and mastering C++'s complexity @ CppCon 2021
int main() {
int arr[] = {1, 2, 3, 3, 2, 1};
std::set unique_values{std::begin(arr), std::end(arr)};
for(auto val : unique_values) {
std::cout << val << ' ';
}
}
Essence or Accident?
68
Understanding and mastering C++'s complexity @ CppCon 2021
int main() {
int arr[] = {1, 2, 3, 3, 2, 1};
std::set unique_values{std::begin(arr), std::end(arr)};
for(auto val : unique_values) {
std::cout << val << ' ';
}
}
Someone = Andrei Zissu
Essence or Accident?
69
Understanding and mastering C++'s complexity @ CppCon 2021
int main() {
int arr[] = {1, 2, 3, 3, 2, 1};
std::set unique_values{std::begin(arr), std::end(arr)};
for(auto val : unique_values) {
std::cout << val << ' ';
}
}
Essence or Accident?
70
Understanding and mastering C++'s complexity @ CppCon 2021
How to store a value obtained from a vector `pop_back()`?
Essence or Accident?
71
Understanding and mastering C++'s complexity @ CppCon 2021
How to store a value obtained from a vector `pop_back()`?
auto val = vec.back();
vec.pop_back();
^ Maybe?
Essence or Accident?
72
Understanding and mastering C++'s complexity @ CppCon 2021
How to store a value obtained from a vector `pop_back()`?
auto val = vec.back();
vec.pop_back();
^ Maybe? Not really...
Essence or Accident?
73
Understanding and mastering C++'s complexity @ CppCon 2021
How to store a value obtained from a vector `pop_back()`?
auto val = std::move(vec.back());
vec.pop_back();
Essence or Accident?
74
Understanding and mastering C++'s complexity @ CppCon 2021
Essence or Accident?
75
Understanding and mastering C++'s complexity @ CppCon 2021
std::vector<bool> flags;
// ...
// need to toggle all flags
for(auto&&flag: flags) {
flag = !flag;
}
Essence or Accident?
76
Understanding and mastering C++'s complexity @ CppCon 2021
std::vector<bool> flags;
// ...
// need to toggle all flags
for(auto&& flag: flags) {
flag = !flag;
}
Essence or Accident?
77
Understanding and mastering C++'s complexity @ CppCon 2021
Essence or Accident?
78
Understanding and mastering C++'s complexity @ CppCon 2021
struct A {
int foo(int) { return 7; }
};
struct B: A {
int foo(float) { return 8; }
};
int main() {
return B().foo(0); // 8 or 7 ?
}
Essence or Accident?
79
Understanding and mastering C++'s complexity @ CppCon 2021
Essence or Accident?
80
Understanding and mastering C++'s complexity @ CppCon 2021
template<typename T>
std::enable_if_t<std::is_integral_v<T>> f(T t) {
// integral version
}
template<typename T>
std::enable_if_t<std::is_floating_point_v<T>> f(T t) {
// floating point version
}
Essence or Accident?
81
Understanding and mastering C++'s complexity @ CppCon 2021
template<typename T>
std::enable_if_t<std::is_integral_v<T>> f(T t) {
// integral version
}
template<typename T>
std::enable_if_t<std::is_floating_point_v<T>> f(T t) {
// floating point version
}
C++20
void f(std::integral auto t) {
//integral version
}
void f(std::floating_point auto t) {
//floating point version
}
Essence or Accident?
82
Understanding and mastering C++'s complexity @ CppCon 2021
Essence or Accident?
83
Understanding and mastering C++'s complexity @ CppCon 2021
std::string s = "but I have heard it works even if you don’t believe in it";
s.replace(0, 4, "").replace(s.find("even"), 4, "only").replace(s.find(" don’t"), 6, "");
assert(s == "I have heard it works only if you believe in it");
Essence or Accident?
84
Understanding and mastering C++'s complexity @ CppCon 2021
std::string s = "but I have heard it works even if you don’t believe in it";
s.replace(0, 4, "").replace(s.find("even"), 4, "only").replace(s.find(" don’t"), 6, "");
assert(s == "I have heard it works only if you believe in it");
Chaining is fixed, but only since C++17: http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0145r3.pdf
Very relevant to the pipe | syntax used by ranges
C++ Principles (Stroustrup, C++ Design and Evolution)
85
Understanding and mastering C++'s complexity @ CppCon 2021
Static type system�- equal support for builtins and user � defined types�- value and reference semantics
Resource and Memory management�- RAII - scoped based�- No garbage collector
Efficient Object Oriented Programming
Flexible and efficient generic programming
Pay only for what you need
Direct access to OS and HW
Leave no room for a lower-level language below C++�* except assembler
See:�The Design of C++, by Bjarne Stroustrup, 1994
The Acronyms
86
Understanding and mastering C++'s complexity @ CppCon 2021
The Acronyms - partial list
87
Understanding and mastering C++'s complexity @ CppCon 2021
RAII
IILE
ODR
ADL
RVO
IFNDR
UB
CTAD
CRTP
CWG
LTO
EBO
RTTI
SFINAE
TU
UDL
The Acronyms - partial list
88
Understanding and mastering C++'s complexity @ CppCon 2021
RAII
IILE
ODR
ADL
RVO
IFNDR
UB
CTAD
CRTP
CWG
LTO
EBO
RTTI
SFINAE
TU
UDL
It’s not complex… just go to the C++ acronym glossary by Arthur O'Dwyer
The Acronyms - partial list
89
Understanding and mastering C++'s complexity @ CppCon 2021
RAII
IILE
ODR
ADL
RVO
IFNDR
UB
CTAD
CRTP
CWG
LTO
EBO
RTTI
SFINAE
TU
UDL
Or join Bob Steagall’s talk here at CppCon 2021 on Friday afternoon
The Acronyms - partial list
90
Understanding and mastering C++'s complexity @ CppCon 2021
RAII
IILE
ODR
ADL
RVO
IFNDR
UB
CTAD
CRTP
CWG
LTO
EBO
RTTI
SFINAE
TU
UDL
Watch also Kate Gregory’s great talk “It’s Complicated” from Meeting C++17
The Pyramid of C++ Knowledge
91
Understanding and mastering C++'s complexity @ CppCon 2021
Applicative C++ Developers
Internal framework and utility maintainers
Library and framework implementers
Language Lawyers
The bare minimum to be a C++ programmer
92
Understanding and mastering C++'s complexity @ CppCon 2021
The bare minimum to be a C++ programmer
93
Understanding and mastering C++'s complexity @ CppCon 2021
the basic syntax, implicit casting rules, const correctness, constexpr,�RAII, Rule of Zero, Rule of Three, operators overloading,�static variables and static members,�RValue and move semantics, Rule of Five,�inheritance, polymorphism, multiple inheritance, virtual inheritance,�exceptions, basic templates, variadic templates,�forwarding reference and perfect forwarding,�std containers, std algorithms, function objects, lambda,�use of smart pointers
The bare minimum to be a C++ programmer
94
Understanding and mastering C++'s complexity @ CppCon 2021
the basic syntax, implicit casting rules, const correctness, constexpr,�RAII, Rule of Zero, Rule of Three, operators overloading,�static variables and static members,�RValue and move semantics, Rule of Five,�inheritance, polymorphism, multiple inheritance, virtual inheritance,�exceptions, basic templates, variadic templates,�forwarding reference and perfect forwarding,�std containers, std algorithms, function objects, lambda,�use of smart pointers
The bare minimum to be a C++ programmer
95
Understanding and mastering C++'s complexity @ CppCon 2021
the basic syntax, implicit casting rules, const correctness, constexpr,�RAII, Rule of Zero, Rule of Three, operators overloading,�static variables and static members,�RValue and move semantics, Rule of Five,�inheritance, polymorphism, multiple inheritance, virtual inheritance,�exceptions, basic templates, variadic templates,�forwarding reference and perfect forwarding,�std containers, std algorithms, function objects, lambda,�use of smart pointers
reading code
browsing cppreference and stackoverflow
Being able to read is important, even crucial
96
Understanding and mastering C++'s complexity @ CppCon 2021
Being able to read C++ code is even more important than writing
Interviewing for a C++ junior position
97
Understanding and mastering C++'s complexity @ CppCon 2021
Interviewing for a C++ junior position
98
Understanding and mastering C++'s complexity @ CppCon 2021
Knows the bare minimum, or we are ready to train.
Interviewing for a C++ junior position
99
Understanding and mastering C++'s complexity @ CppCon 2021
Knows the bare minimum, or we are ready to train.
Loves programming. Really, loves programming!
Interviewing for a C++ junior position
100
Understanding and mastering C++'s complexity @ CppCon 2021
Knows the bare minimum, or we are ready to train.
Loves programming. Really, loves programming!
Interviewing for a C++ junior position
101
Understanding and mastering C++'s complexity @ CppCon 2021
Knows the bare minimum, or we are ready to train.
Loves programming. Really, loves programming!
Implications of innocent ignorance
102
Understanding and mastering C++'s complexity @ CppCon 2021
Well, I didn’t know that…
Implications of innocent ignorance
103
Understanding and mastering C++'s complexity @ CppCon 2021
Well, I didn’t know that…
Implications of innocent ignorance
104
Understanding and mastering C++'s complexity @ CppCon 2021
Well, I didn’t know that…
still positive value
negative value
Improving your C++ level
105
Understanding and mastering C++'s complexity @ CppCon 2021
Improving your C++ level
106
Understanding and mastering C++'s complexity @ CppCon 2021
Curiosity - in C++ everything has a reason, try to figure it out
Improving your C++ level
107
Understanding and mastering C++'s complexity @ CppCon 2021
Curiosity - in C++ everything has a reason, try to figure it out
Read, listen, watch
Improving your C++ level
108
Understanding and mastering C++'s complexity @ CppCon 2021
Curiosity - in C++ everything has a reason, try to figure it out
Read, listen, watch
^ Read Q&A in Stackoverflow
Improving your C++ level
109
Understanding and mastering C++'s complexity @ CppCon 2021
Curiosity - in C++ everything has a reason, try to figure it out
Read, listen, watch
^ Read Q&A in Stackoverflow
^ Ask in Stackoverflow
Improving your C++ level
110
Understanding and mastering C++'s complexity @ CppCon 2021
Curiosity - in C++ everything has a reason, try to figure it out
Read, listen, watch
^ Read Q&A in Stackoverflow
^ Ask in Stackoverflow
^ Answer in Stackoverflow
Improving while answering in SO
111
Understanding and mastering C++'s complexity @ CppCon 2021
112
Understanding and mastering C++'s complexity @ CppCon 2021
113
Understanding and mastering C++'s complexity @ CppCon 2021
114
Understanding and mastering C++'s complexity @ CppCon 2021
115
Understanding and mastering C++'s complexity @ CppCon 2021
116
Understanding and mastering C++'s complexity @ CppCon 2021
117
Understanding and mastering C++'s complexity @ CppCon 2021
118
Understanding and mastering C++'s complexity @ CppCon 2021
119
Understanding and mastering C++'s complexity @ CppCon 2021
120
Understanding and mastering C++'s complexity @ CppCon 2021
Improving your C++ level
121
Understanding and mastering C++'s complexity @ CppCon 2021
Remember, it’s a never ending mission
122
Understanding and mastering C++'s complexity @ CppCon 2021
�void conclude(auto greetings) {� while(still_time() && have_questions()) {� ask();� }� greetings();�}
conclude([]{ std::cout << "Thank you!"; });
Thank you!
Other Essence or Accident
out due to lack of time
123
Understanding and mastering C++'s complexity @ CppCon 2021
Essence or Accident?
124
Understanding and mastering C++'s complexity @ CppCon 2021
template<class T, long Numerator, long Denominator, long MultNum, long MultDenom>
auto constexpr operator*(Aggregator<T, Numerator, Denominator> a,
std::ratio<MultNum, MultDenom> n) {
if constexpr(Numerator*MultNum != Denominator*MultDenom) {
return Aggregator<T, Numerator * MultNum, Denominator * MultDenom> { a };
} else {
return a.unsafe_multiply(n);
}
}
Source: The Point Challenge https://www.youtube.com/watch?v=wNGEtlBSCLY
Essence or Accident?
125
Understanding and mastering C++'s complexity @ CppCon 2021
Implement methods for rotating the x,y,z fields in Pixel struct below:
struct Pixel {
int x;
int y;
int z;
};
Essence or Accident?
126
Understanding and mastering C++'s complexity @ CppCon 2021
class Permutation {
std::array<int Pixel::*, 3> permutation;
constexpr Permutation(int Pixel::* a, int Pixel::* b, int Pixel::* c)
: permutation{a, b, c} {}
public:
static constexpr Permutation xzy() { return {&Pixel::x, &Pixel::z, &Pixel::y}; }
constexpr Pixel permutate(Pixel p) const {
Pixel permutated;
permutated.x = p.*permutation[0];
permutated.y = p.*permutation[1];
permutated.z = p.*permutation[2];
return permutated;
}
};