![Page 2: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/2.jpg)
2
Klaus Iglberger
C++ Trainer since 2016
Author of the C++ math library
(Co-)Organizer of the Munich C++ user group
Regular presenter at C++ conferences
Email: [email protected]
![Page 3: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/3.jpg)
3
What is C++?
![Page 4: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/4.jpg)
4
![Page 5: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/5.jpg)
What is C++?
5
![Page 6: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/6.jpg)
Whatis C++?
kind of programminglanguage
6
![Page 7: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/7.jpg)
7
![Page 8: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/8.jpg)
8
![Page 9: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/9.jpg)
9
“… Overall you can get up and running pretty quickly with an object-oriented programming
language like C++. …”
Brad Larson, Episode 234 of CppCast, February, 13th, 2020
![Page 10: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/10.jpg)
10
![Page 11: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/11.jpg)
11
![Page 12: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/12.jpg)
12
![Page 13: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/13.jpg)
13
![Page 14: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/14.jpg)
14
![Page 15: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/15.jpg)
15
![Page 16: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/16.jpg)
16
![Page 17: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/17.jpg)
17
![Page 18: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/18.jpg)
18
![Page 19: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/19.jpg)
19
![Page 20: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/20.jpg)
20
What kind of programming language is C++?
![Page 21: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/21.jpg)
21
Procedural programming
Object-oriented programming
Functional programming
Generic programming
Multiparadigm programming
![Page 22: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/22.jpg)
22
Let’s see …
![Page 23: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/23.jpg)
23
Disclaimer
C++ is too complex to give a complete answer
I consider only solutions with dynamic polymorphism
I consider only the state of the art of C++
There will be a lot of code …
… but it’s not about the details, it’s about the design
I deliberately choose a simple problem …
![Page 24: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/24.jpg)
24
Our Toy Problem: Drawing Shapes
![Page 25: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/25.jpg)
enum ShapeType { circle, square };
class Shape { public: explicit Shape( ShapeType t ) : type{ t } {}
virtual ~Shape() = default; ShapeType getType() const noexcept;
private: ShapeType type; };
class Circle : public Shape { public: explicit Circle( double rad ) : Shape{ circle } , radius{ rad } , // ... Remaining data members {}
virtual ~Circle() = default;
25
A Procedural Solution
![Page 26: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/26.jpg)
enum ShapeType { circle, square };
class Shape { public: explicit Shape( ShapeType t ) : type{ t } {}
virtual ~Shape() = default; ShapeType getType() const noexcept;
private: ShapeType type; };
class Circle : public Shape { public: explicit Circle( double rad ) : Shape{ circle } , radius{ rad } , // ... Remaining data members {}
virtual ~Circle() = default;
26
A Procedural Solution
![Page 27: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/27.jpg)
enum ShapeType { circle, square };
class Shape { public: explicit Shape( ShapeType t ) , type{ t } {}
virtual ~Shape() = default; ShapeType getType() const noexcept;
private: ShapeType type; };
class Circle : public Shape { public: explicit Circle( double rad ) : Shape{ circle } , radius{ rad } , // ... Remaining data members {}
virtual ~Circle() = default;
27
A Procedural Solution
![Page 28: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/28.jpg)
explicit Shape( ShapeType t ) , type{ t } {}
virtual ~Shape() = default; ShapeType getType() const noexcept;
private: ShapeType type; };
class Circle : public Shape { public: explicit Circle( double rad ) : Shape{ circle } , radius{ rad } , // ... Remaining data members {}
virtual ~Circle() = default; double getRadius() const noexcept; // ... getCenter(), getRotation(), ...
private: double radius; // ... Remaining data members };
void translate( Circle&, Vector3D const& ); void rotate( Circle&, Quaternion const& ); void draw( Circle const& );
class Square : public Shape { public: explicit Square( double s ) : Shape{ square } , side{ s }
28
A Procedural Solution
![Page 29: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/29.jpg)
// ... getCenter(), getRotation(), ...
private: double radius; // ... Remaining data members };
void translate( Circle&, Vector3D const& ); void rotate( Circle&, Quaternion const& ); void draw( Circle const& );
class Square : public Shape { public: explicit Square( double s ) : Shape{ square } , side{ s } , // ... Remaining data members {}
virtual ~Square() = default; double getSide() const noexcept; // ... getCenter(), getRotation(), ...
private: double side; // ... Remaining data members };
void translate( Square&, Vector3D const& ); void rotate( Square&, Quaternion const& ); void draw( Square const& );
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { switch ( s->type ) { case circle:
29
A Procedural Solution
![Page 30: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/30.jpg)
{}
virtual ~Square() = default; double getSide() const noexcept; // ... getCenter(), getRotation(), ...
private: double side; // ... Remaining data members };
void translate( Square&, Vector3D const& ); void rotate( Square&, Quaternion const& ); void draw( Square const& );
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { switch ( s->type ) { case circle: DrawCircle( *static_cast<Circle const*>( s.get() ) ); break; case square: DrawSquare( *static_cast<Square const*>( s.get() ) ); break; } } }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0 ) ); shapes.push_back( std::make_unique<Square>( 1.5 ) ); shapes.push_back( std::make_unique<Circle>( 4.2 ) );
30
A Procedural Solution
![Page 31: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/31.jpg)
{ for( auto const& s : shapes ) { switch ( s->type ) { case circle: DrawCircle( *static_cast<Circle const*>( s.get() ) ); break; case square: DrawSquare( *static_cast<Square const*>( s.get() ) ); break; } } }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0 ) ); shapes.push_back( std::make_unique<Square>( 1.5 ) ); shapes.push_back( std::make_unique<Circle>( 4.2 ) );
// Drawing all shapes draw( shapes ); }
31
A Procedural Solution
![Page 32: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/32.jpg)
32
Design Evaluation
Enum
1 = very bad, …, 9 = very good
![Page 33: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/33.jpg)
33
Design Evaluation
Addition of shapes (OCP)
Enum
1 = very bad, …, 9 = very good
![Page 34: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/34.jpg)
34
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Enum
1 = very bad, …, 9 = very good
![Page 35: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/35.jpg)
35
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP)
Enum
1 = very bad, …, 9 = very good
![Page 36: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/36.jpg)
36
The SOLID Principles
Single Responsibility Principle
Open-Closed Principle
Liskov Substitution Principle
Interface Segregation Principle
Dependency Inversion Principle
![Page 37: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/37.jpg)
37
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP)
Enum
1 = very bad, …, 9 = very good
![Page 38: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/38.jpg)
38
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use
Enum
1 = very bad, …, 9 = very good
![Page 39: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/39.jpg)
39
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum
1 = very bad, …, 9 = very good
![Page 40: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/40.jpg)
40
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1
1 = very bad, …, 9 = very good
![Page 41: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/41.jpg)
41
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7
1 = very bad, …, 9 = very good
![Page 42: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/42.jpg)
42
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5
1 = very bad, …, 9 = very good
![Page 43: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/43.jpg)
43
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6
1 = very bad, …, 9 = very good
![Page 44: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/44.jpg)
44
Performance Results
Benchmarks!
MacBook Pro 13-inch, MacOS 10.14.6 (Mojave), Intel Core i7, 2,7Ghz, 16 GByte, Compilation flags: -std=c++17 -O3 -DNDEBUG
100 random shapes, 2.5M updates each, normalized to the enum solution
![Page 45: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/45.jpg)
Enum
Relative Runtime
0 1 2 3 4 5
GCC-9.2 Clang-9
45
Performance Results
MacBook Pro 13-inch, MacOS 10.14.6 (Mojave), Intel Core i7, 2,7Ghz, 16 GByte, Compilation flags: -std=c++17 -O3 -DNDEBUG
100 random shapes, 2.5M updates each, normalized to the enum solution
![Page 46: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/46.jpg)
46
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
1 = very bad, …, 9 = very good
![Page 47: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/47.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void translate( Vector3D const& ) = 0; virtual void rotate( Quaternion const& ) = 0; virtual void draw() const = 0; };
class Circle : public Shape { public: explicit Circle( double rad ) : radius{ rad } , // ... Remaining data members {}
virtual ~Circle() = default;
double getRadius() const noexcept; // ... getCenter(), getRotation(), ...
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double radius;
47
An Object-Oriented Solution
![Page 48: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/48.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void translate( Vector3D const& ) = 0; virtual void rotate( Quaternion const& ) = 0; virtual void draw() const = 0; };
class Circle : public Shape { public: explicit Circle( double rad ) : radius{ rad } , // ... Remaining data members {}
virtual ~Circle() = default;
double getRadius() const noexcept; // ... getCenter(), getRotation(), ...
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double radius;
48
An Object-Oriented Solution
![Page 49: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/49.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void translate( Vector3D const& ) = 0; virtual void rotate( Quaternion const& ) = 0; virtual void draw() const = 0; };
class Circle : public Shape { public: explicit Circle( double rad ) : radius{ rad } , // ... Remaining data members {}
virtual ~Circle() = default;
double getRadius() const noexcept; // ... getCenter(), getRotation(), ...
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double radius; // ... Remaining data members };
class Square : public Shape { public: explicit Square( double s ) : side{ s }
49
An Object-Oriented Solution
![Page 50: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/50.jpg)
double getRadius() const noexcept; // ... getCenter(), getRotation(), ...
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double radius; // ... Remaining data members };
class Square : public Shape { public: explicit Square( double s ) : side{ s } , // ... Remaining data members {}
virtual ~Square() = default;
double getSide() const noexcept; // ... getCenter(), getRotation(), ...
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double side; // ... Remaining data members };
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->draw() }
50
An Object-Oriented Solution
![Page 51: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/51.jpg)
, // ... Remaining data members {}
virtual ~Square() = default;
double getSide() const noexcept; // ... getCenter(), getRotation(), ...
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double side; // ... Remaining data members };
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->draw() } }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0 ) ); shapes.push_back( std::make_unique<Square>( 1.5 ) ); shapes.push_back( std::make_unique<Circle>( 4.2 ) );
// Drawing all shapes draw( shapes ); }
51
An Object-Oriented Solution
![Page 52: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/52.jpg)
void draw() const override;
private: double side; // ... Remaining data members };
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->draw() } }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0 ) ); shapes.push_back( std::make_unique<Square>( 1.5 ) ); shapes.push_back( std::make_unique<Circle>( 4.2 ) );
// Drawing all shapes draw( shapes ); }
52
An Object-Oriented Solution
![Page 53: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/53.jpg)
53
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO
1 = very bad, …, 9 = very good
![Page 54: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/54.jpg)
54
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8
1 = very bad, …, 9 = very good
![Page 55: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/55.jpg)
55
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2
1 = very bad, …, 9 = very good
![Page 56: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/56.jpg)
56
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2
1 = very bad, …, 9 = very good
![Page 57: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/57.jpg)
57
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6
1 = very bad, …, 9 = very good
![Page 58: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/58.jpg)
58
Performance Results
Enum
OO
Relative Runtime
0 1 2 3 4 5
GCC-9.2 Clang-9
MacBook Pro 13-inch, MacOS 10.14.6 (Mojave), Intel Core i7, 2,7Ghz, 16 GByte, Compilation flags: -std=c++17 -O3 -DNDEBUG
100 random shapes, 2.5M updates each, normalized to the enum solution
![Page 59: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/59.jpg)
59
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
1 = very bad, …, 9 = very good
![Page 60: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/60.jpg)
60
The Visitor Design Pattern
Client
virtual visit( ConcreteElementA ) = 0 virtual visit( ConcreteElementB ) = 0
virtual accept( Visitor ) = 0
ObjectStructure
Visitor Element
ConcreteVisitorA
virtual visit( ConcreteElementA ) virtual visit( ConcreteElementB )
ConcreteVisitorB
virtual visit( ConcreteElementA ) virtual visit( ConcreteElementB )
ConcreteElementA
virtual accept( Visitor v ) operationA()
ConcreteElementB
virtual accept( Visitor v ) operationB()
v->visit( this ) v->visit( this )
![Page 61: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/61.jpg)
61
The Visitor Design Pattern
Client
virtual visit( Circle ) = 0 virtual visit( Square ) = 0
virtual accept( Visitor ) = 0
ObjectStructure
Visitor Shape
ConcreteVisitorA
virtual visit( Circle ) virtual visit( Square )
ConcreteVisitorB
virtual visit( Circle ) virtual visit( Square )
Circle
virtual accept( Visitor v ) operationA()
Square
virtual accept( Visitor v ) operationB()
v->visit( this ) v->visit( this )
![Page 62: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/62.jpg)
62
The Visitor Design Pattern
Client
virtual visit( Circle ) = 0 virtual visit( Square ) = 0
virtual accept( Visitor ) = 0
ObjectStructure
Visitor Shape
Rotate
virtual visit( Circle ) virtual visit( Square )
Draw
virtual visit( Circle ) virtual visit( Square )
Circle
virtual accept( Visitor v ) operationA()
Square
virtual accept( Visitor v ) operationB()
v->visit( this ) v->visit( this )
![Page 63: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/63.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void accept( Visitor const& ) = 0; };
class Circle : public Shape { public: explicit Circle( double rad ) : radius{ rad } , // ... Remaining data members {}
virtual ~Circle() = default;
class Circle; class Square;
class Visitor { public: virtual ~Visitor() = default;
virtual void visit( Circle const& ) const = 0; virtual void visit( Square const& ) const = 0; };
63
A Visitor-Based Solution
![Page 64: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/64.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void accept( Visitor const& ) = 0; };
class Circle : public Shape { public: explicit Circle( double rad ) : radius{ rad } , // ... Remaining data members {}
virtual ~Circle() = default;
class Circle; class Square;
class Visitor { public: virtual ~Visitor() = default;
virtual void visit( Circle const& ) const = 0; virtual void visit( Square const& ) const = 0; };
64
A Visitor-Based Solution
![Page 65: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/65.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void accept( Visitor const& ) = 0; };
class Circle : public Shape { public: explicit Circle( double rad ) : radius{ rad } , // ... Remaining data members {}
virtual ~Circle() = default;
class Circle; class Square;
class Visitor { public: virtual ~Visitor() = default;
virtual void visit( Circle const& ) const = 0; virtual void visit( Square const& ) const = 0; };
65
A Visitor-Based Solution
![Page 66: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/66.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void accept( Visitor const& ) = 0; };
class Circle : public Shape { public: explicit Circle( double rad ) : radius{ rad } , // ... Remaining data members {}
virtual ~Circle() = default;
double getRadius() const noexcept; // ... getCenter(), getRotation(), ...
void accept( Visitor const& ) override;
private: double radius; // ... Remaining data members };
class Square : public Shape { public: explicit Square( double s ) : side{ s } , // ... Remaining data members {}
};
66
A Visitor-Based Solution
![Page 67: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/67.jpg)
virtual ~Circle() = default;
double getRadius() const noexcept; // ... getCenter(), getRotation(), ...
void accept( Visitor const& ) override;
private: double radius; // ... Remaining data members };
class Square : public Shape { public: explicit Square( double s ) : side{ s } , // ... Remaining data members {}
virtual ~Square() = default;
double getSide() const noexcept; // ... getCenter(), getRotation(), ...
void accept( Visitor const& ) override;
private: double side; // ... Remaining data members };
class Draw : public Visitor { public: void visit( Circle const& ) const override; void visit( Square const& ) const override; };
67
A Visitor-Based Solution
![Page 68: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/68.jpg)
virtual ~Square() = default;
double getSide() const noexcept; // ... getCenter(), getRotation(), ...
void accept( Visitor const& ) override;
private: double side; // ... Remaining data members };
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->accept( Draw{} ) } }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0 ) ); shapes.push_back( std::make_unique<Square>( 1.5 ) ); shapes.push_back( std::make_unique<Circle>( 4.2 ) );
class Draw : public Visitor { public: void visit( Circle const& ) const override; void visit( Square const& ) const override; };
68
A Visitor-Based Solution
![Page 69: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/69.jpg)
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->accept( Draw{} ) } }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0 ) ); shapes.push_back( std::make_unique<Square>( 1.5 ) ); shapes.push_back( std::make_unique<Circle>( 4.2 ) );
// Drawing all shapes draw( shapes ); }
class Draw : public Visitor { public: void visit( Circle const& ) const override; void visit( Square const& ) const override; };
69
A Visitor-Based Solution
![Page 70: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/70.jpg)
70
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor
1 = very bad, …, 9 = very good
![Page 71: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/71.jpg)
71
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2
1 = very bad, …, 9 = very good
![Page 72: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/72.jpg)
72
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8
1 = very bad, …, 9 = very good
![Page 73: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/73.jpg)
73
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8
1 = very bad, …, 9 = very good
![Page 74: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/74.jpg)
74
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3
1 = very bad, …, 9 = very good
![Page 75: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/75.jpg)
75
Performance Results
Enum
OO
Visitor
Relative Runtime
0 1 2 3 4 5
GCC-9.2 Clang-9
MacBook Pro 13-inch, MacOS 10.14.6 (Mojave), Intel Core i7, 2,7Ghz, 16 GByte, Compilation flags: -std=c++17 -O3 -DNDEBUG
100 random shapes, 2.5M updates each, normalized to the enum solution
![Page 76: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/76.jpg)
76
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
1 = very bad, …, 9 = very good
![Page 77: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/77.jpg)
class Circle { public: explicit Circle( double rad ) : radius{ rad } , // ... Remaining data members {}
double getRadius() const noexcept; // ... getCenter(), getRotation(), ...
private: double radius; // ... Remaining data members };
class Square { public: explicit Square( double s ) : side{ s } , // ... Remaining data members {}
double getSide() const noexcept; // ... getCenter(), getRotation(), ...
private: double side; // ... Remaining data members
77
A “Modern C++” Solution
![Page 78: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/78.jpg)
class Circle { public: explicit Circle( double rad ) : radius{ rad } , // ... Remaining data members {}
double getRadius() const noexcept; // ... getCenter(), getRotation(), ...
private: double radius; // ... Remaining data members };
class Square { public: explicit Square( double s ) : side{ s } , // ... Remaining data members {}
double getSide() const noexcept; // ... getCenter(), getRotation(), ...
private: double side; // ... Remaining data members
78
A “Modern C++” Solution
![Page 79: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/79.jpg)
explicit Circle( double rad ) : radius{ rad } , // ... Remaining data members {}
double getRadius() const noexcept; // ... getCenter(), getRotation(), ...
private: double radius; // ... Remaining data members };
class Square { public: explicit Square( double s ) : side{ s } , // ... Remaining data members {}
double getSide() const noexcept; // ... getCenter(), getRotation(), ...
private: double side; // ... Remaining data members };
using Shape = std::variant<Circle,Square>;
class Draw { public: void operator()( Circle const& ) const override; void operator()( Square const& ) const override; };
79
A “Modern C++” Solution
![Page 80: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/80.jpg)
public: explicit Square( double s ) : side{ s } , // ... Remaining data members {}
double getSide() const noexcept; // ... getCenter(), getRotation(), ...
private: double side; // ... Remaining data members };
using Shape = std::variant<Circle,Square>;
void draw( std::vector<Shape> const& shapes ) { for( auto const& s : shapes ) { std::visit( Draw{}, s ); } }
int main() { using Shapes = std::vector<Shape>;
// Creating some shapes Shapes shapes; shapes.push_back( Circle{ 2.0 } ); shapes.push_back( Square{ 1.5 } );
class Draw { public: void operator()( Circle const& ) const override; void operator()( Square const& ) const override; };
80
A “Modern C++” Solution
![Page 81: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/81.jpg)
using Shape = std::variant<Circle,Square>;
void draw( std::vector<Shape> const& shapes ) { for( auto const& s : shapes ) { std::visit( Draw{}, s ); } }
int main() { using Shapes = std::vector<Shape>;
// Creating some shapes Shapes shapes; shapes.push_back( Circle{ 2.0 } ); shapes.push_back( Square{ 1.5 } ); shapes.push_back( Circle{ 4.2 } );
// Drawing all shapes draw( shapes ); }
public: void operator()( Circle const& ) const override; void operator()( Square const& ) const override; };
81
A “Modern C++” Solution
![Page 82: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/82.jpg)
82
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
std::variant
1 = very bad, …, 9 = very good
![Page 83: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/83.jpg)
83
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
std::variant 3
1 = very bad, …, 9 = very good
![Page 84: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/84.jpg)
84
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
std::variant 3 9
1 = very bad, …, 9 = very good
![Page 85: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/85.jpg)
85
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
std::variant 3 9 9
1 = very bad, …, 9 = very good
![Page 86: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/86.jpg)
86
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
std::variant 3 9 9 9
1 = very bad, …, 9 = very good
![Page 87: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/87.jpg)
87
Performance Results
Enum
OO
Visitor
std::variant
Relative Runtime
0 1 2 3 4 5
GCC-9.2 Clang-9
MacBook Pro 13-inch, MacOS 10.14.6 (Mojave), Intel Core i7, 2,7Ghz, 16 GByte, Compilation flags: -std=c++17 -O3 -DNDEBUG
100 random shapes, 2.5M updates each, normalized to the enum solution
![Page 88: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/88.jpg)
template< typename T, typename V >constexpr auto make_func() { return + []( const char* b, V v ) { const auto& x = *reinterpret_cast<const T*>(b); v(x); };}
template< typename… Ts, typename V >void foo( std::size_t i, const char* b, V v ) { static constexpr std::array<void(*)(const char*, V v ), sizeof…(Ts)> table = { make_func<Ts,V>()... }; table[i](b,v);}
88
Performance Analysis
![Page 89: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/89.jpg)
89
mpark::variant
![Page 90: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/90.jpg)
90
Performance Results
Enum
OO
Visitor
std::variant
mpark::variant
Relative Runtime
0 1 2 3 4 5
GCC-9.2 Clang-9
MacBook Pro 13-inch, MacOS 10.14.6 (Mojave), Intel Core i7, 2,7Ghz, 16 GByte, Compilation flags: -std=c++17 -O3 -DNDEBUG
100 random shapes, 2.5M updates each, normalized to the enum solution
![Page 91: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/91.jpg)
// ...
switch (v.index()) { case B + 0: return MPARK_DISPATCH(B + 0); case B + 1: return MPARK_DISPATCH(B + 1); case B + 2: return MPARK_DISPATCH(B + 2); case B + 3: return MPARK_DISPATCH(B + 3); case B + 4: return MPARK_DISPATCH(B + 4); case B + 5: return MPARK_DISPATCH(B + 5); case B + 6: return MPARK_DISPATCH(B + 6); case B + 7: return MPARK_DISPATCH(B + 7); case B + 8: return MPARK_DISPATCH(B + 8); case B + 9: return MPARK_DISPATCH(B + 9); case B + 10: return MPARK_DISPATCH(B + 10); case B + 11: return MPARK_DISPATCH(B + 11); case B + 12: return MPARK_DISPATCH(B + 12); case B + 13: return MPARK_DISPATCH(B + 13); case B + 14: return MPARK_DISPATCH(B + 14); case B + 15: return MPARK_DISPATCH(B + 15); case B + 16: return MPARK_DISPATCH(B + 16); case B + 17: return MPARK_DISPATCH(B + 17); case B + 18: return MPARK_DISPATCH(B + 18); case B + 19: return MPARK_DISPATCH(B + 19); case B + 20: return MPARK_DISPATCH(B + 20); case B + 21: return MPARK_DISPATCH(B + 21); case B + 22: return MPARK_DISPATCH(B + 22); case B + 23: return MPARK_DISPATCH(B + 23); case B + 24: return MPARK_DISPATCH(B + 24); case B + 25: return MPARK_DISPATCH(B + 25); case B + 26: return MPARK_DISPATCH(B + 26); case B + 27: return MPARK_DISPATCH(B + 27);
91
Performance Analysis
![Page 92: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/92.jpg)
case B + 2: return MPARK_DISPATCH(B + 2); case B + 3: return MPARK_DISPATCH(B + 3); case B + 4: return MPARK_DISPATCH(B + 4); case B + 5: return MPARK_DISPATCH(B + 5); case B + 6: return MPARK_DISPATCH(B + 6); case B + 7: return MPARK_DISPATCH(B + 7); case B + 8: return MPARK_DISPATCH(B + 8); case B + 9: return MPARK_DISPATCH(B + 9); case B + 10: return MPARK_DISPATCH(B + 10); case B + 11: return MPARK_DISPATCH(B + 11); case B + 12: return MPARK_DISPATCH(B + 12); case B + 13: return MPARK_DISPATCH(B + 13); case B + 14: return MPARK_DISPATCH(B + 14); case B + 15: return MPARK_DISPATCH(B + 15); case B + 16: return MPARK_DISPATCH(B + 16); case B + 17: return MPARK_DISPATCH(B + 17); case B + 18: return MPARK_DISPATCH(B + 18); case B + 19: return MPARK_DISPATCH(B + 19); case B + 20: return MPARK_DISPATCH(B + 20); case B + 21: return MPARK_DISPATCH(B + 21); case B + 22: return MPARK_DISPATCH(B + 22); case B + 23: return MPARK_DISPATCH(B + 23); case B + 24: return MPARK_DISPATCH(B + 24); case B + 25: return MPARK_DISPATCH(B + 25); case B + 26: return MPARK_DISPATCH(B + 26); case B + 27: return MPARK_DISPATCH(B + 27); case B + 28: return MPARK_DISPATCH(B + 28); case B + 29: return MPARK_DISPATCH(B + 29); case B + 30: return MPARK_DISPATCH(B + 30); case B + 31: return MPARK_DISPATCH(B + 31); default: return MPARK_DEFAULT(B + 32); }
// ...
92
Performance Analysis
![Page 93: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/93.jpg)
93
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
1 = very bad, …, 9 = very good
![Page 94: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/94.jpg)
Whatis C++?
kind of programminglanguage
94
![Page 95: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/95.jpg)
95
![Page 96: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/96.jpg)
96
Solution Analysis
⚪No Pointers
⚪No Virtual Functions
⚪No Manual Allocations
⚪(Cache) Efficient⚪Value
Semantics
⚪Simple
⚪SRP
⚪OCP
⚪Loose Coupling
![Page 97: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/97.jpg)
97
![Page 98: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/98.jpg)
98
![Page 99: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/99.jpg)
99
The Strategy Design Pattern
Context
context()
Strategy
virtual algorithm() = 0
ConcreteStrategyA
virtual algorithm()
ConcreteStrategyB
strategy
virtual algorithm()
![Page 100: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/100.jpg)
100
The Strategy Design Pattern
Shape
draw()
DrawStrategy
virtual draw() = 0
DrawStrategyA
virtual draw()
DrawStrategyB
strategy
virtual draw()
![Page 101: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/101.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void translate( Vector3D const& ) = 0; virtual void rotate( Quaternion const& ) = 0; virtual void draw() const = 0; };
class Circle : public Shape { public: explicit Circle( double rad, std::unique_ptr<DrawStrategy> ds ) : radius{ rad } , // ... Remaining data members
class Circle; class Square;
class DrawStrategy { public: virtual ~DrawStrategy() {}
virtual void draw( const Circle& circle ) const = 0; virtual void draw( const Square& square ) const = 0; };
101
A Strategy-Based Solution
![Page 102: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/102.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void translate( Vector3D const& ) = 0; virtual void rotate( Quaternion const& ) = 0; virtual void draw() const = 0; };
class Circle : public Shape { public: explicit Circle( double rad, std::unique_ptr<DrawStrategy> ds ) : radius{ rad } , // ... Remaining data members
class Circle; class Square;
class DrawStrategy { public: virtual ~DrawStrategy() {}
virtual void draw( const Circle& circle ) const = 0; virtual void draw( const Square& square ) const = 0; };
102
A Strategy-Based Solution
![Page 103: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/103.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void translate( Vector3D const& ) = 0; virtual void rotate( Quaternion const& ) = 0; virtual void draw() const = 0; };
class Circle : public Shape { public: explicit Circle( double rad, std::unique_ptr<DrawStrategy> ds ) : radius{ rad } , // ... Remaining data members , drawing{ std::move(ds) } {}
virtual ~Circle() = default;
class Circle; class Square;
class DrawStrategy { public: virtual ~DrawStrategy() {}
virtual void draw( const Circle& circle ) const = 0; virtual void draw( const Square& square ) const = 0; };
103
A Strategy-Based Solution
![Page 104: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/104.jpg)
{ public: Shape() = default; virtual ~Shape() = default; virtual void translate( Vector3D const& ) = 0; virtual void rotate( Quaternion const& ) = 0; virtual void draw() const = 0; };
class Circle : public Shape { public: explicit Circle( double rad, std::unique_ptr<DrawStrategy> ds ) : radius{ rad } , // ... Remaining data members , drawing{ std::move(ds) } {}
virtual ~Circle() = default;
double getRadius() const noexcept; // ... getCenter(), getRotation(), ...
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double radius; // ... Remaining data members std:unique_ptr<DrawStrategy> drawing; };
class Square : public Shape { public: explicit Square( double s, std::unique_ptr<DrawStrategy> ds )
104
A Strategy-Based Solution
![Page 105: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/105.jpg)
void rotate( Quaternion const& ) override; void draw() const override;
private: double radius; // ... Remaining data members std:unique_ptr<DrawStrategy> drawing; };
class Square : public Shape { public: explicit Square( double s, std::unique_ptr<DrawStrategy> ds ) : side{ s } , // ... Remaining data members , drawing{ std::move(ds) } {}
virtual ~Square() = default;
double getSide() const noexcept; // ... getCenter(), getRotation(), ...
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double side; // ... Remaining data members std::unique_ptr<DrawStrategy> drawing; };
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->draw()
105
A Strategy-Based Solution
![Page 106: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/106.jpg)
{}
virtual ~Square() = default;
double getSide() const noexcept; // ... getCenter(), getRotation(), ...
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double side; // ... Remaining data members std::unique_ptr<DrawStrategy> drawing; };
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->draw() } }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
class OpenGLStrategy : public DrawStrategy { public: virtual ~OpenGLStrategy() {}
void draw( Circle const& circle ) const override; void draw( Square const& square ) const override; };
106
A Strategy-Based Solution
![Page 107: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/107.jpg)
void rotate( Quaternion const& ) override; void draw() const override;
private: double side; // ... Remaining data members std::unique_ptr<DrawStrategy> drawing; };
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->draw() } }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0 , std::make_unique<OpenGLStrategy>() ) ); shapes.push_back( std::make_unique<Square>( 1.5 , std::make_unique<OpenGLStrategy>() ) ); shapes.push_back( std::make_unique<Circle>( 4.2 , std::make_unique<OpenGLStrategy>() ) );
class OpenGLStrategy : public DrawStrategy { public: virtual ~OpenGLStrategy() {}
void draw( Circle const& circle ) const override; void draw( Square const& square ) const override; };
107
A Strategy-Based Solution
![Page 108: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/108.jpg)
{ s->draw() } }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0 , std::make_unique<OpenGLStrategy>() ) ); shapes.push_back( std::make_unique<Square>( 1.5 , std::make_unique<OpenGLStrategy>() ) ); shapes.push_back( std::make_unique<Circle>( 4.2 , std::make_unique<OpenGLStrategy>() ) );
// Drawing all shapes draw( shapes ); }
class OpenGLStrategy : public DrawStrategy { public: virtual ~OpenGLStrategy() {}
void draw( Circle const& circle ) const override; void draw( Square const& square ) const override; };
108
A Strategy-Based Solution
![Page 109: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/109.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void translate( Vector3D const& ) = 0; virtual void rotate( Quaternion const& ) = 0; virtual void draw() const = 0; };
class Circle : public Shape { public: explicit Circle( double rad, std::unique_ptr<DrawStrategy> ds ) : radius{ rad } , // ... Remaining data members
class Circle; class Square;
class DrawStrategy { public: virtual ~DrawStrategy() {}
virtual void draw( const Circle& circle ) const = 0; virtual void draw( const Square& square ) const = 0; };
109
A Strategy-Based Solution
![Page 110: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/110.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void translate( Vector3D const& ) = 0; virtual void rotate( Quaternion const& ) = 0; virtual void draw() const = 0; };
class Circle; class Square;
class DrawCircleStrategy { public: virtual ~DrawCircleStrategy() {}
virtual void draw( const Circle& circle ) const = 0; };
class DrawSquareStrategy { public: virtual ~DrawSquareStrategy() {}
virtual void draw( const Square& square ) const = 0; };
110
A Strategy-Based Solution
![Page 111: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/111.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void translate( Vector3D const& ) = 0; virtual void rotate( Quaternion const& ) = 0; virtual void draw() const = 0; };
class Circle : public Shape { public: explicit Circle( double rad, std::unique_ptr<DrawCircleStrategy> ds ) : radius{ rad } , // ... Remaining data members , drawing{ std::move(ds) } {}
virtual ~Circle() = default;
double getRadius() const noexcept; // ... getCenter(), getRotation(), ...
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double radius; // ... Remaining data members std:unique_ptr<DrawCircleStrategy> drawing; };
class Square : public Shape { public:
111
A Strategy-Based Solution
![Page 112: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/112.jpg)
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double radius; // ... Remaining data members std:unique_ptr<DrawCircleStrategy> drawing; };
class Square : public Shape { public: explicit Square( double s, std::unique_ptr<DrawSquareStrategy> ds ) : side{ s } , // ... Remaining data members , drawing{ std::move(ds) } {}
virtual ~Square() = default;
double getSide() const noexcept; // ... getCenter(), getRotation(), ...
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double side; // ... Remaining data members std::unique_ptr<DrawSquareStrategy> drawing; };
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->draw()
112
A Strategy-Based Solution
![Page 113: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/113.jpg)
double side; // ... Remaining data members std::unique_ptr<DrawSquareStrategy> drawing; };
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->draw() } }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0 , std::make_unique<OpenGLCircleStrategy>() ) ); shapes.push_back( std::make_unique<Square>( 1.5
class OpenGLCircleStrategy : public DrawCircleStrategy { public: virtual ~OpenGLStrategy() {}
void draw( Circle const& circle ) const override; };
class OpenGLSquareStrategy : public DrawSquareStrategy { public: virtual ~OpenGLStrategy() {}
void draw( Square const& square ) const override; };
113
A Strategy-Based Solution
![Page 114: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/114.jpg)
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0 , std::make_unique<OpenGLCircleStrategy>() ) ); shapes.push_back( std::make_unique<Square>( 1.5 , std::make_unique<OpenGLSquareStrategy>() ) ); shapes.push_back( std::make_unique<Circle>( 4.2 , std::make_unique<OpenGLCircleStrategy>() ) );
// Drawing all shapes draw( shapes ); }
public: virtual ~OpenGLStrategy() {}
void draw( Circle const& circle ) const override; };
class OpenGLSquareStrategy : public DrawSquareStrategy { public: virtual ~OpenGLStrategy() {}
void draw( Square const& square ) const override; };
114
A Strategy-Based Solution
![Page 115: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/115.jpg)
115
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy
1 = very bad, …, 9 = very good
![Page 116: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/116.jpg)
116
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7
1 = very bad, …, 9 = very good
![Page 117: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/117.jpg)
117
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2
1 = very bad, …, 9 = very good
![Page 118: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/118.jpg)
118
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2 7
1 = very bad, …, 9 = very good
![Page 119: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/119.jpg)
119
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2 7 4
1 = very bad, …, 9 = very good
![Page 120: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/120.jpg)
120
Performance Results
Enum
OO
Visitor
std::variant
mpark::variant
Strategy
Relative Runtime
0 1 2 3 4 5
GCC-9.2 Clang-9
MacBook Pro 13-inch, MacOS 10.14.6 (Mojave), Intel Core i7, 2,7Ghz, 16 GByte, Compilation flags: -std=c++17 -O3 -DNDEBUG
100 random shapes, 2.5M updates each, normalized to the enum solution
![Page 121: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/121.jpg)
121
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2 7 4 2
1 = very bad, …, 9 = very good
![Page 122: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/122.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void translate( Vector3D const& ) = 0; virtual void rotate( Quaternion const& ) = 0; virtual void draw() const = 0; };
class Circle : public Shape { public: explicit Circle( double rad, DrawCircleStrategy ds ) : radius{ rad } , // ... Remaining data members , drawing{ std::move(ds) } {}
virtual ~Circle() = default;
double getRadius() const noexcept;
class Circle; class Square;
using DrawCircleStrategy = std::function<void(Circle const&)>; using DrawSquareStrategy = std::function<void(Square const&)>;
122
A “Modern C++” Solution
![Page 123: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/123.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void translate( Vector3D const& ) = 0; virtual void rotate( Quaternion const& ) = 0; virtual void draw() const = 0; };
class Circle : public Shape { public: explicit Circle( double rad, DrawCircleStrategy ds ) : radius{ rad } , // ... Remaining data members , drawing{ std::move(ds) } {}
virtual ~Circle() = default;
double getRadius() const noexcept;
class Circle; class Square;
using DrawCircleStrategy = std::function<void(Circle const&)>; using DrawSquareStrategy = std::function<void(Square const&)>;
123
A “Modern C++” Solution
![Page 124: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/124.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void translate( Vector3D const& ) = 0; virtual void rotate( Quaternion const& ) = 0; virtual void draw() const = 0; };
class Circle : public Shape { public: explicit Circle( double rad, DrawCircleStrategy ds ) : radius{ rad } , // ... Remaining data members , drawing{ std::move(ds) } {}
virtual ~Circle() = default;
double getRadius() const noexcept;
class Circle; class Square;
using DrawCircleStrategy = std::function<void(Circle const&)>; using DrawSquareStrategy = std::function<void(Square const&)>;
124
A “Modern C++” Solution
![Page 125: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/125.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void translate( Vector3D const& ) = 0; virtual void rotate( Quaternion const& ) = 0; virtual void draw() const = 0; };
class Circle : public Shape { public: explicit Circle( double rad, DrawCircleStrategy ds ) : radius{ rad } , // ... Remaining data members , drawing{ std::move(ds) } {}
virtual ~Circle() = default;
double getRadius() const noexcept; // ... getCenter(), getRotation(), ...
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double radius; // ... Remaining data members DrawCircleStrategy drawing; };
class Square : public Shape { public:
125
A “Modern C++” Solution
![Page 126: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/126.jpg)
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double radius; // ... Remaining data members DrawCircleStrategy drawing; };
class Square : public Shape { public: explicit Square( double s, DrawSquareStrategy ds ) : side{ s } , // ... Remaining data members , drawing{ std::move(ds) } {}
virtual ~Square() = default;
double getSide() const noexcept; // ... getCenter(), getRotation(), ...
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double side; // ... Remaining data members DrawSquareStrategy drawing; };
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) {
126
A “Modern C++” Solution
![Page 127: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/127.jpg)
private: double side; // ... Remaining data members DrawSquareStrategy drawing; };
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->draw() } }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0 , OpenGLCircleStrategy{} ) ); shapes.push_back( std::make_unique<Square>( 1.5 , OpenGLSquareStrategy{} ) ); shapes.push_back( std::make_unique<Circle>( 4.2
class OpenGLCircleStrategy { public: void operator()( Circle const& circle ) const; };
class OpenGLSquareStrategy { public: void operator()( Square const& square ) const; };
127
A “Modern C++” Solution
![Page 128: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/128.jpg)
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0 , OpenGLCircleStrategy{} ) ); shapes.push_back( std::make_unique<Square>( 1.5 , OpenGLSquareStrategy{} ) ); shapes.push_back( std::make_unique<Circle>( 4.2 , OpenGLCircleStrategy{} ) );
// Drawing all shapes draw( shapes ); }
class OpenGLCircleStrategy { public: void operator()( Circle const& circle ) const; };
class OpenGLSquareStrategy { public: void operator()( Square const& square ) const; };
128
A “Modern C++” Solution
![Page 129: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/129.jpg)
private: double side; // ... Remaining data members DrawSquareStrategy drawing; };
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->draw() } }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0 , OpenGLCircleStrategy{} ) ); shapes.push_back( std::make_unique<Square>( 1.5 , OpenGLSquareStrategy{} ) ); shapes.push_back( std::make_unique<Circle>( 4.2
class OpenGLCircleStrategy { public: void operator()( Circle const& circle ) const; };
class OpenGLSquareStrategy { public: void operator()( Square const& square ) const; };
129
A “Modern C++” Solution
![Page 130: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/130.jpg)
private: double side; // ... Remaining data members DrawSquareStrategy drawing; };
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->draw() } }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0, Draw{} ) ); shapes.push_back( std::make_unique<Square>( 1.5, Draw{} ) ); shapes.push_back( std::make_unique<Circle>( 4.2, Draw{} ) );
// Drawing all shapes
void draw( Circle const& circle ) const; void draw( Square const& square ) const;
struct Draw { template< typename T > void operator()( T const& drawable ) const { draw( drawable ); } };
130
A “Modern C++” Solution
![Page 131: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/131.jpg)
} }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0, Draw{} ) ); shapes.push_back( std::make_unique<Square>( 1.5, Draw{} ) ); shapes.push_back( std::make_unique<Circle>( 4.2, Draw{} ) );
// Drawing all shapes draw( shapes ); }
void draw( Circle const& circle ) const; void draw( Square const& square ) const;
struct Draw { template< typename T > void operator()( T const& drawable ) const { draw( drawable ); } };
131
A “Modern C++” Solution
![Page 132: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/132.jpg)
private: double side; // ... Remaining data members DrawSquareStrategy drawing; };
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->draw() } }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0, Draw{} ) ); shapes.push_back( std::make_unique<Square>( 1.5, Draw{} ) ); shapes.push_back( std::make_unique<Circle>( 4.2, Draw{} ) );
// Drawing all shapes
void draw( Circle const& circle ) const; void draw( Square const& square ) const;
struct Draw { template< typename T > void operator()( T const& drawable ) const { draw( drawable ); } };
132
A “Modern C++” Solution
![Page 133: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/133.jpg)
private: double side; // ... Remaining data members DrawSquareStrategy drawing; };
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->draw() } }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0, Draw{} ) ); shapes.push_back( std::make_unique<Square>( 1.5, Draw{} ) ); shapes.push_back( std::make_unique<Circle>( 4.2, Draw{} ) );
// Drawing all shapes
void draw( Circle const& circle ) const; void draw( Square const& square ) const;
133
A “Modern C++” Solution
![Page 134: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/134.jpg)
class Shape { public: Shape() = default; virtual ~Shape() = default; virtual void translate( Vector3D const& ) = 0; virtual void rotate( Quaternion const& ) = 0; virtual void draw() const = 0; };
class Circle : public Shape { public: explicit Circle( double rad, DrawCircleStrategy ds = Draw{} )
class Circle; class Square;
using DrawCircleStrategy = std::function<void(Circle const&)>; using DrawSquareStrategy = std::function<void(Square const&)>;
struct Draw { template< typename T > void operator()( T const& drawable ) const { draw( drawable ); } };
134
A “Modern C++” Solution
![Page 135: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/135.jpg)
{ public: Shape() = default; virtual ~Shape() = default; virtual void translate( Vector3D const& ) = 0; virtual void rotate( Quaternion const& ) = 0; virtual void draw() const = 0; };
class Circle : public Shape { public: explicit Circle( double rad, DrawCircleStrategy ds = Draw{} ) : radius{ rad } , // ... Remaining data members , drawing{ std::move(ds) } {}
virtual ~Circle() = default;
double getRadius() const noexcept; // ... getCenter(), getRotation(), ...
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double radius; // ... Remaining data members DrawCircleStrategy drawing; };
class Square : public Shape { public: explicit Square( double s, DrawSquareStrategy ds = Draw{} )
135
A “Modern C++” Solution
![Page 136: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/136.jpg)
void rotate( Quaternion const& ) override; void draw() const override;
private: double radius; // ... Remaining data members DrawCircleStrategy drawing; };
class Square : public Shape { public: explicit Square( double s, DrawSquareStrategy ds = Draw{} ) : side{ s } , // ... Remaining data members , drawing{ std::move(ds) } {}
virtual ~Square() = default;
double getSide() const noexcept; // ... getCenter(), getRotation(), ...
void translate( Vector3D const& ) override; void rotate( Quaternion const& ) override; void draw() const override;
private: double side; // ... Remaining data members DrawSquareStrategy drawing; };
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->draw()
136
A “Modern C++” Solution
![Page 137: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/137.jpg)
void draw( std::vector<std::unique_ptr<Shape>> const& shapes ) { for( auto const& s : shapes ) { s->draw() } }
int main() { using Shapes = std::vector<std::unique_ptr<Shape>>;
// Creating some shapes Shapes shapes; shapes.push_back( std::make_unique<Circle>( 2.0 ) ); shapes.push_back( std::make_unique<Square>( 1.5 ) ); shapes.push_back( std::make_unique<Circle>( 4.2 ) );
// Drawing all shapes draw( shapes ); }
void draw( Circle const& circle ) const; void draw( Square const& square ) const;
137
A “Modern C++” Solution
![Page 138: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/138.jpg)
138
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2 7 4 2
std::function
1 = very bad, …, 9 = very good
![Page 139: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/139.jpg)
139
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2 7 4 2
std::function 8
1 = very bad, …, 9 = very good
![Page 140: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/140.jpg)
140
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2 7 4 2
std::function 8 3
1 = very bad, …, 9 = very good
![Page 141: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/141.jpg)
141
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2 7 4 2
std::function 8 3 7
1 = very bad, …, 9 = very good
![Page 142: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/142.jpg)
142
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2 7 4 2
std::function 8 3 7 7
1 = very bad, …, 9 = very good
![Page 143: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/143.jpg)
143
Performance Results
Enum
OO
Visitor
std::variant
mpark::variant
Strategy
std::function
Relative Runtime
0 1 2 3 4 5
GCC-9.2 Clang-9
MacBook Pro 13-inch, MacOS 10.14.6 (Mojave), Intel Core i7, 2,7Ghz, 16 GByte, Compilation flags: -std=c++17 -O3 -DNDEBUG
100 random shapes, 2.5M updates each, normalized to the enum solution
![Page 144: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/144.jpg)
144
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2 7 4 2
std::function 8 3 7 7 5
1 = very bad, …, 9 = very good
![Page 145: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/145.jpg)
class Circle { public: explicit Circle( double rad ) : radius{ rad } , // ... Remaining data members {}
double getRadius() const noexcept; // ... getCenter(), getRotation(), ...
private: double radius; // ... Remaining data members };
void translate( Circle const&, Vector3D const& ); void rotate( Circle const&, Quaternion const& ); void draw( Circle const& );
class Square { public: explicit Square( double s ) : side{ s } , // ... Remaining data members {}
double getSide() const noexcept;
145
A Type-Erased Solution
![Page 146: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/146.jpg)
class Circle { public: explicit Circle( double rad ) : radius{ rad } , // ... Remaining data members {}
double getRadius() const noexcept; // ... getCenter(), getRotation(), ...
private: double radius; // ... Remaining data members };
void translate( Circle const&, Vector3D const& ); void rotate( Circle const&, Quaternion const& ); void draw( Circle const& );
class Square { public: explicit Square( double s ) : side{ s } , // ... Remaining data members {}
double getSide() const noexcept;
146
A Type-Erased Solution
![Page 147: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/147.jpg)
// ... getCenter(), getRotation(), ...
private: double radius; // ... Remaining data members };
void translate( Circle const&, Vector3D const& ); void rotate( Circle const&, Quaternion const& ); void draw( Circle const& );
class Square { public: explicit Square( double s ) : side{ s } , // ... Remaining data members {}
double getSide() const noexcept; // ... getCenter(), getRotation(), ...
private: double side; // ... Remaining data members };
void translate( Square const&, Vector3D const& ); void rotate( Square const&, Quaternion const& ); void draw( Square const& );
class Shape { private: struct Concept { virtual ~Concept() {} virtual void do_translate( Vector3D const& v ) const = 0;
147
A Type-Erased Solution
![Page 148: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/148.jpg)
double getSide() const noexcept; // ... getCenter(), getRotation(), ...
private: double side; // ... Remaining data members };
void translate( Square const&, Vector3D const& ); void rotate( Square const&, Quaternion const& ); void draw( Square const& );
class Shape { private: struct Concept { virtual ~Concept() {} virtual void do_translate( Vector3D const& v ) const = 0; virtual void do_rotate( Quaternion const& q ) const = 0; virtual void do_draw() const = 0; // ... };
template< typename T > struct Model : Concept { Model( T const& value ) : object{ value } {}
void do_translate( Vector3D const& v ) const override { translate( object, v ); } void do_rotate( Quaternion const& q ) const override { rotate( object, q ); }
148
A Type-Erased Solution
![Page 149: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/149.jpg)
struct Concept { virtual ~Concept() {} virtual void do_translate( Vector3D const& v ) const = 0; virtual void do_rotate( Quaternion const& q ) const = 0; virtual void do_draw() const = 0; // ... };
template< typename T > struct Model : Concept { Model( T const& value ) : object{ value } {}
void do_translate( Vector3D const& v ) const override { translate( object, v ); } void do_rotate( Quaternion const& q ) const override { rotate( object, q ); }
void do_draw() const override { draw( object ); }
// ...
T object; };
std::unique_ptr<Concept> pimpl; friend void translate( Shape& shape, Vector3D const& v ) {
149
A Type-Erased Solution
![Page 150: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/150.jpg)
double getSide() const noexcept; // ... getCenter(), getRotation(), ...
private: double side; // ... Remaining data members };
void translate( Square const&, Vector3D const& ); void rotate( Square const&, Quaternion const& ); void draw( Square const& );
class Shape { private: struct Concept { virtual ~Concept() {} virtual void do_translate( Vector3D const& v ) const = 0; virtual void do_rotate( Quaternion const& q ) const = 0; virtual void do_draw() const = 0; // ... };
template< typename T > struct Model : Concept { Model( T const& value ) : object{ value } {}
void do_translate( Vector3D const& v ) const override { translate( object, v ); } void do_rotate( Quaternion const& q ) const override { rotate( object, q ); }
150
A Type-Erased Solution
![Page 151: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/151.jpg)
}
void do_draw() const override { draw( object ); }
// ...
T object; };
std::unique_ptr<Concept> pimpl; friend void translate( Shape& shape, Vector3D const& v ) { shape.pimpl->do_translate( v ); } friend void rotate( Shape& shape, Quaternion const& q ) { shape.pimpl->do_rotate( q ); } friend void draw( Shape const& shape ) { shape.pimpl->do_draw(); }
public: template< typename T > Shape( T const& x ) : pimpl{ new Model<T>( x ) } {}
// Special member functions Shape( Shape const& s ); Shape( Shape&& s ); Shape& operator=( Shape const& s ); Shape& operator=( Shape&& s );
151
A Type-Erased Solution
![Page 152: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/152.jpg)
}
void do_draw() const override { draw( object ); }
// ...
T object; };
std::unique_ptr<Concept> pimpl; friend void translate( Shape& shape, Vector3D const& v ) { shape.pimpl->do_translate( v ); } friend void rotate( Shape& shape, Quaternion const& q ) { shape.pimpl->do_rotate( q ); } friend void draw( Shape const& shape ) { shape.pimpl->do_draw(); }
public: template< typename T > Shape( T const& x ) : pimpl{ new Model<T>( x ) } {}
// Special member functions Shape( Shape const& s ); Shape( Shape&& s ); Shape& operator=( Shape const& s ); Shape& operator=( Shape&& s );
152
A Type-Erased Solution
![Page 153: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/153.jpg)
void draw( std::vector<Shape> const& shapes ) { for( auto const& shape : shapes ) { draw( shape ); } }
int main() { using Shapes = std::vector<Shape>;
} friend void rotate( Shape& shape, Quaternion const& q ) { shape.pimpl->do_rotate( q ); } friend void draw( Shape const& shape ) { shape.pimpl->do_draw(); }
public: template< typename T > Shape( T const& x ) : pimpl{ new Model<T>( x ) } {}
// Special member functions Shape( Shape const& s ); Shape( Shape&& s ); Shape& operator=( Shape const& s ); Shape& operator=( Shape&& s );
// ... };
153
A Type-Erased Solution
![Page 154: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/154.jpg)
void draw( std::vector<Shape> const& shapes ) { for( auto const& shape : shapes ) { draw( shape ); } }
int main() { using Shapes = std::vector<Shape>;
// Creating some shapes Shapes shapes; shapes.push_back( Circle( 2.0 ) ); shapes.push_back( Square( 1.5 ) ); shapes.push_back( Circle( 4.2 ) );
// Drawing all shapes draw( shapes ); }
}
public: template< typename T > Shape( T const& x ) : pimpl{ new Model<T>( x ) } {}
// Special member functions Shape( Shape const& s ); Shape( Shape&& s ); Shape& operator=( Shape const& s ); Shape& operator=( Shape&& s );
// ... };
154
A Type-Erased Solution
![Page 155: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/155.jpg)
void draw( std::vector<Shape> const& shapes ) { for( auto const& shape : shapes ) { draw( shape ); } }
int main() { using Shapes = std::vector<Shape>;
// Creating some shapes Shapes shapes; shapes.push_back( Circle( 2.0 ) ); shapes.push_back( Square( 1.5 ) ); shapes.push_back( Circle( 4.2 ) );
// Drawing all shapes draw( shapes ); }
Shape& operator=( Shape const& s ); Shape& operator=( Shape&& s );
// ... };
155
A Type-Erased Solution
![Page 156: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/156.jpg)
156
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2 7 4 2
std::function 8 3 7 7 5
Type Erasure
1 = very bad, …, 9 = very good
![Page 157: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/157.jpg)
157
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2 7 4 2
std::function 8 3 7 7 5
Type Erasure 9
1 = very bad, …, 9 = very good
![Page 158: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/158.jpg)
158
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2 7 4 2
std::function 8 3 7 7 5
Type Erasure 9 4
1 = very bad, …, 9 = very good
![Page 159: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/159.jpg)
159
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2 7 4 2
std::function 8 3 7 7 5
Type Erasure 9 4 8
1 = very bad, …, 9 = very good
![Page 160: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/160.jpg)
160
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2 7 4 2
std::function 8 3 7 7 5
Type Erasure 9 4 8 8
1 = very bad, …, 9 = very good
![Page 161: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/161.jpg)
161
Performance Results
Enum
OO
Visitor
std::variant
mpark::variant
Strategy
std::function
Type Erasure
Relative Runtime
0 1 2 3 4 5
GCC-9.2 Clang-9
MacBook Pro 13-inch, MacOS 10.14.6 (Mojave), Intel Core i7, 2,7Ghz, 16 GByte, Compilation flags: -std=c++17 -O3 -DNDEBUG
100 random shapes, 2.5M updates each, normalized to the enum solution
![Page 162: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/162.jpg)
162
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2 7 4 2
std::function 8 3 7 7 5
Type Erasure 9 4 8 8 6
1 = very bad, …, 9 = very good
![Page 163: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/163.jpg)
163
Design Evaluation
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
Enum 1 7 5 6 9
OO 8 2 2 6 6
Visitor 2 8 8 3 1
mpark::variant 3 9 9 9 9
Strategy 7 2 7 4 2
std::function 8 3 7 7 5
Type Erasure 9 4 8 8 6
1 = very bad, …, 9 = very good
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
mpark::variant 3 9 9 9 9
std::function 8 3 7 7 5
Type Erasure 9 4 8 8 6
![Page 164: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/164.jpg)
164
Design Evaluation
1 = very bad, …, 9 = very good
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
mpark::variant 3 9 9 9 9
std::function 8 3 7 7 5
Type Erasure 9 4 8 8 6
![Page 165: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/165.jpg)
165
Design Evaluation
1 = very bad, …, 9 = very good
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
mpark::variant 3 9 9 9 9
std::function 8 3 7 7 5
Type Erasure 9 4 8 8 6
![Page 166: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/166.jpg)
166
Design Evaluation
1 = very bad, …, 9 = very good
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
mpark::variant 3 9 9 9 9
std::function 8 3 7 7 5
Type Erasure 9 4 8 8 6
![Page 167: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/167.jpg)
167
Design Evaluation
1 = very bad, …, 9 = very good
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
mpark::variant 3 9 9 9 9
std::function 8 3 7 7 5
Type Erasure 9 4 8 8 6
![Page 168: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/168.jpg)
168
Design Evaluation
1 = very bad, …, 9 = very good
Addition of shapes (OCP)
Addition of operations (OCP)
Separation of Concerns (SRP) Ease of Use Performance
mpark::variant 3 9 9 9 9
std::function 8 3 7 7 5
Type Erasure 9 4 8 8 6
![Page 169: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/169.jpg)
169
Procedural programming
Object-oriented programming
Functional programming
Generic programming
Multiparadigm programming
![Page 170: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/170.jpg)
170
No-paradigm programming
![Page 171: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/171.jpg)
171
![Page 172: Embrace No-Paradigm Programming · 2020-04-09 · Embrace No-Paradigm Programming Klaus Iglberger, CppEurope 2020 klaus.iglberger@gmx.de](https://reader033.vdocuments.site/reader033/viewer/2022050418/5f8de21a05f1897f053a0ec1/html5/thumbnails/172.jpg)
Tell Them …
172
Understand the virtues of “Modern C++” Reduce the use of pointers and inheritance hierarchies Prefer value semantics Keep your code simple
Learn about the different programming paradigms Learn about their advantages and weaknesses Pick the good ideas
There is no “one-fits-all” solution
Don’t shackle yourself with thinking about paradigms …