1 of 126

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

2 of 126

Before we .begin()

2

Understanding and mastering C++'s complexity @ CppCon 2021

3 of 126

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

4 of 126

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

5 of 126

Complexity

What this talk is about (and what’s not)

5

Understanding and mastering C++'s complexity @ CppCon 2021

6 of 126

Complexity

This talk is NOT about Algorithmic Complexity (no big ‘O’ in this talk!)

6

Understanding and mastering C++'s complexity @ CppCon 2021

7 of 126

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

8 of 126

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

9 of 126

About Me

9

Understanding and mastering C++'s complexity @ CppCon 2021

10 of 126

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

11 of 126

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

12 of 126

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

13 of 126

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

14 of 126

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

15 of 126

Complexity

Isn’t it the name of the game? (of being a programmer...)

15

Understanding and mastering C++'s complexity @ CppCon 2021

16 of 126

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

17 of 126

Complexity

Is C++ complex?

Why?

Can it be less complex?

17

Understanding and mastering C++'s complexity @ CppCon 2021

18 of 126

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:

19 of 126

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:

20 of 126

What makes C++ complex - for you?

20

Understanding and mastering C++'s complexity @ CppCon 2021

Let’s review the questionnaire results

21 of 126

Complexity

21

Understanding and mastering C++'s complexity @ CppCon 2021

22 of 126

Complexity

Why programming is complex?

22

Understanding and mastering C++'s complexity @ CppCon 2021

23 of 126

What makes a software language complex?

23

Understanding and mastering C++'s complexity @ CppCon 2021

24 of 126

What makes a software language complex?

  • Too many ways for doing the same thing (“too many options”)

24

Understanding and mastering C++'s complexity @ CppCon 2021

25 of 126

What makes a software language complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)

25

Understanding and mastering C++'s complexity @ CppCon 2021

26 of 126

What makes a software language complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community

26

Understanding and mastering C++'s complexity @ CppCon 2021

27 of 126

What makes a software language complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too low level
    • Too high-level, abstract
    • Contradicting paradigms or rules
    • Rules are not intuitive or too complicated

27

Understanding and mastering C++'s complexity @ CppCon 2021

28 of 126

What makes a software language complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too low level
    • Too high-level, abstract
    • Contradicting paradigms or rules
    • Rules are not intuitive or too complicated
  • Complex problems

28

Understanding and mastering C++'s complexity @ CppCon 2021

29 of 126

What makes a software language complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too low level
    • Too high-level, abstract
    • Contradicting paradigms or rules
    • Rules are not intuitive or too complicated
  • Complex problems
  • New syntax, new stuff getting into the language

29

Understanding and mastering C++'s complexity @ CppCon 2021

30 of 126

What makes a software language complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too low level
    • Too high-level, abstract
    • Contradicting paradigms or rules
    • Rules are not intuitive or too complicated
  • Complex problems
  • New syntax, new stuff getting into the language
  • Backward compatibility issues between language versions

30

Understanding and mastering C++'s complexity @ CppCon 2021

31 of 126

What makes a software language complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too low level
    • Too high-level, abstract
    • Contradicting paradigms or rules
    • Rules are not intuitive or too complicated
  • Complex problems
  • New syntax, new stuff getting into the language
  • Backward compatibility issues between language versions
  • Lack of proper tools

31

Understanding and mastering C++'s complexity @ CppCon 2021

32 of 126

What makes a software language complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too low level
    • Too high-level, abstract
    • Contradicting paradigms or rules
    • Rules are not intuitive or too complicated
  • Complex problems
  • New syntax, new stuff getting into the language
  • Backward compatibility issues between language versions
  • Lack of proper tools

32

Understanding and mastering C++'s complexity @ CppCon 2021

33 of 126

Complexity

Is C++ complex?

Why?

Can it be less complex?

33

Understanding and mastering C++'s complexity @ CppCon 2021

34 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too low level
    • Too high-level, abstract
    • Contradicting paradigms or rules
    • Rules are not intuitive or too complicated
  • Complex problems
  • New syntax, new stuff getting into the language
  • Backward compatibility issues between language versions
  • Lack of proper tools

34

Understanding and mastering C++'s complexity @ CppCon 2021

35 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)?
  • Too few ways for doing things (“hard to express yourself fluently”)?

35

Understanding and mastering C++'s complexity @ CppCon 2021

36 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)

36

Understanding and mastering C++'s complexity @ CppCon 2021

37 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community?

37

Understanding and mastering C++'s complexity @ CppCon 2021

38 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community

38

Understanding and mastering C++'s complexity @ CppCon 2021

39 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too low level
    • Too high-level, abstract
    • Contradicting paradigms or rules
    • Rules are not intuitive or too complicated

39

Understanding and mastering C++'s complexity @ CppCon 2021

40 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too Has quite a few low level features
    • Too Has quite a few high-level features
    • Has some contradicting paradigms or rules
    • Some rules are not intuitive or too complicated

40

Understanding and mastering C++'s complexity @ CppCon 2021

41 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too Has quite a few low level features
    • Too Has quite a few high-level features
    • Has some contradicting paradigms or rules
    • Some rules are not intuitive or too complicated
  • Complex problems?

41

Understanding and mastering C++'s complexity @ CppCon 2021

42 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too Has quite a few low level features
    • Too Has quite a few high-level features
    • Has some contradicting paradigms or rules
    • Some rules are not intuitive or too complicated
  • Deals frequently with complex problems

42

Understanding and mastering C++'s complexity @ CppCon 2021

43 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too Has quite a few low level features
    • Too Has quite a few high-level features
    • Has some contradicting paradigms or rules
    • Some rules are not intuitive or too complicated
  • Deals frequently with complex problems
  • New syntax, new stuff getting into the language?

43

Understanding and mastering C++'s complexity @ CppCon 2021

44 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too Has quite a few low level features
    • Too Has quite a few high-level features
    • Has some contradicting paradigms or rules
    • Some rules are not intuitive or too complicated
  • Deals frequently with complex problems
  • New syntax, new stuff getting into the language

44

Understanding and mastering C++'s complexity @ CppCon 2021

45 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too Has quite a few low level features
    • Too Has quite a few high-level features
    • Has some contradicting paradigms or rules
    • Some rules are not intuitive or too complicated
  • Deals frequently with complex problems
  • New syntax, new stuff getting into the language
  • Backward compatibility issues between language versions?

45

Understanding and mastering C++'s complexity @ CppCon 2021

46 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too Has quite a few low level features
    • Too Has quite a few high-level features
    • Has some contradicting paradigms or rules
    • Some rules are not intuitive or too complicated
  • Deals frequently with complex problems
  • New syntax, new stuff getting into the language
  • Backward compatibility issues between language versions

46

Understanding and mastering C++'s complexity @ CppCon 2021

47 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too Has quite a few low level features
    • Too Has quite a few high-level features
    • Has some contradicting paradigms or rules
    • Some rules are not intuitive or too complicated
  • Deals frequently with complex problems
  • New syntax, new stuff getting into the language
  • Backward compatibility issues between language versions
  • Lack of proper tools?

47

Understanding and mastering C++'s complexity @ CppCon 2021

48 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too Has quite a few low level features
    • Too Has quite a few high-level features
    • Has some contradicting paradigms or rules
    • Some rules are not intuitive or too complicated
  • Deals frequently with complex problems
  • New syntax, new stuff getting into the language
  • Backward compatibility issues between language versions
  • Lack of proper tools. But improving!

48

Understanding and mastering C++'s complexity @ CppCon 2021

49 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too Has quite a few low level features
    • Too Has quite a few high-level features
    • Has some contradicting paradigms or rules
    • Some rules are not intuitive or too complicated
  • Deals frequently with complex problems
  • New syntax, new stuff getting into the language
  • Backward compatibility issues between language versions
  • Lack of proper tools. But improving!

49

Understanding and mastering C++'s complexity @ CppCon 2021

50 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too Has quite a few low level features
    • Too Has quite a few high-level features
    • Has some contradicting paradigms or rules
    • Some rules are not intuitive or too complicated
  • Deals frequently with complex problems
  • New syntax, new stuff getting into the language
  • Backward compatibility issues between language versions
  • Lack of proper tools. But improving!

50

Understanding and mastering C++'s complexity @ CppCon 2021

51 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too Has quite a few low level features
    • Too Has quite a few high-level features
    • Has some contradicting paradigms or rules
    • Some rules are not intuitive or too complicated
  • Deals frequently with complex problems
  • New syntax, new stuff getting into the language
  • Backward compatibility issues between language versions
  • Lack of proper tools. But improving!

51

Understanding and mastering C++'s complexity @ CppCon 2021

52 of 126

What makes C++ complex?

  • Too many ways for doing the same thing (“too many options”)
  • Too few ways for doing things (“hard to express yourself fluently”)
  • Lack of standard / documentation / proper examples / community
  • Complex model
    • Too Has quite a few low level features
    • Too Has quite a few high-level features
    • Has some contradicting paradigms or rules
    • Some rules are not intuitive or too complicated
  • Deals frequently with complex problems
  • New syntax, new stuff getting into the language
  • Backward compatibility issues between language versions
  • Lack of proper tools. But improving!

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??

53 of 126

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

54 of 126

Essence or Accident?

54

Understanding and mastering C++'s complexity @ CppCon 2021

55 of 126

Essence or Accident?

55

Understanding and mastering C++'s complexity @ CppCon 2021

Let’s play...

56 of 126

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);

57 of 126

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

58 of 126

Essence or Accident?

58

Understanding and mastering C++'s complexity @ CppCon 2021

59 of 126

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;

};

60 of 126

Can the compiler deduce that a class *needs*�a virtual destructor and provide one?

60

Understanding and mastering C++'s complexity @ CppCon 2021

61 of 126

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”?

62 of 126

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;

};

63 of 126

Essence or Accident?

63

Understanding and mastering C++'s complexity @ CppCon 2021

64 of 126

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;

};

// ...

};

65 of 126

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;

};

// ...

};

66 of 126

Essence or Accident?

66

Understanding and mastering C++'s complexity @ CppCon 2021

67 of 126

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 << ' ';

}

}

68 of 126

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

69 of 126

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 << ' ';

}

}

70 of 126

Essence or Accident?

70

Understanding and mastering C++'s complexity @ CppCon 2021

How to store a value obtained from a vector `pop_back()`?

71 of 126

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?

72 of 126

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...

73 of 126

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();

74 of 126

Essence or Accident?

74

Understanding and mastering C++'s complexity @ CppCon 2021

75 of 126

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;

}

76 of 126

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;

}

77 of 126

Essence or Accident?

77

Understanding and mastering C++'s complexity @ CppCon 2021

78 of 126

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 ?

}

79 of 126

Essence or Accident?

79

Understanding and mastering C++'s complexity @ CppCon 2021

80 of 126

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

}

81 of 126

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

}

82 of 126

Essence or Accident?

82

Understanding and mastering C++'s complexity @ CppCon 2021

83 of 126

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");

84 of 126

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

85 of 126

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

86 of 126

The Acronyms

86

Understanding and mastering C++'s complexity @ CppCon 2021

87 of 126

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

88 of 126

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

89 of 126

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

90 of 126

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

91 of 126

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

92 of 126

The bare minimum to be a C++ programmer

92

Understanding and mastering C++'s complexity @ CppCon 2021

93 of 126

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

94 of 126

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

95 of 126

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

96 of 126

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

  • know what you know
  • know what you don’t know
  • learn

97 of 126

Interviewing for a C++ junior position

97

Understanding and mastering C++'s complexity @ CppCon 2021

98 of 126

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.

99 of 126

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!

100 of 126

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!

Smart and gets things done.

101 of 126

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!

Smart and gets things done.

102 of 126

Implications of innocent ignorance

102

Understanding and mastering C++'s complexity @ CppCon 2021

Well, I didn’t know that…

103 of 126

Implications of innocent ignorance

103

Understanding and mastering C++'s complexity @ CppCon 2021

Well, I didn’t know that…

  • Less elegant code (harder to maintain, harder to read)
  • Less Generic code (could be written in a more generic way)
  • Not being able to implement things
  • Inefficient code
  • Bug prone
  • Actual bug!

104 of 126

Implications of innocent ignorance

104

Understanding and mastering C++'s complexity @ CppCon 2021

Well, I didn’t know that…

  • Less elegant code (harder to maintain, harder to read)
  • Less Generic code (could be written in a more generic way)
  • Not being able to implement things
  • Inefficient code
  • Bug prone
  • Actual bug!

still positive value

negative value

105 of 126

Improving your C++ level

105

Understanding and mastering C++'s complexity @ CppCon 2021

106 of 126

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

107 of 126

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

108 of 126

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

109 of 126

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

110 of 126

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

111 of 126

Improving while answering in SO

111

Understanding and mastering C++'s complexity @ CppCon 2021

112 of 126

112

Understanding and mastering C++'s complexity @ CppCon 2021

113 of 126

113

Understanding and mastering C++'s complexity @ CppCon 2021

114 of 126

114

Understanding and mastering C++'s complexity @ CppCon 2021

115 of 126

115

Understanding and mastering C++'s complexity @ CppCon 2021

116 of 126

116

Understanding and mastering C++'s complexity @ CppCon 2021

117 of 126

117

Understanding and mastering C++'s complexity @ CppCon 2021

118 of 126

118

Understanding and mastering C++'s complexity @ CppCon 2021

119 of 126

119

Understanding and mastering C++'s complexity @ CppCon 2021

120 of 126

120

Understanding and mastering C++'s complexity @ CppCon 2021

121 of 126

Improving your C++ level

121

Understanding and mastering C++'s complexity @ CppCon 2021

Remember, it’s a never ending mission

122 of 126

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!

123 of 126

Other Essence or Accident

out due to lack of time

123

Understanding and mastering C++'s complexity @ CppCon 2021

124 of 126

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

125 of 126

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;

};

126 of 126

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;

}

};