Slide 1

Slide 1 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 1/119 Type Safe C++? - LOL! :-) Björn Fahller

Slide 2

Slide 2 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 2/119 Type Safe C++? - LOL! :-) Björn Fahller

Slide 3

Slide 3 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 3/119 Type Safe C++? - LOL! :-) What is type safety?

Slide 4

Slide 4 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 4/119 What is type safety? type safety (Noun) the extent to which a programming language discourages or prevents type errors -- Wiktionary

Slide 5

Slide 5 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 5/119 A type safe system discourages or prevents... ● ... use of one type when another is intended ● ... operations that do not make sense ● ... use of values outside the defined space

Slide 6

Slide 6 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 6/119 ● Introduction to type safety ● Type safety in C++ ● Simple library solution for strong types ● Sophisticated libraries – scouting github! ● What strong types does with your code Type Safe C++? - LOL! :-)

Slide 7

Slide 7 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 7/119 using request_id = uint32_t; using receiver_id = uint32_t; token remove(request_id req, receiver_id rec); token initiate_remove(receiver_id receiver) { auto req = new_request(); return remove(receiver, req); } My story begins

Slide 8

Slide 8 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 8/119 using request_id = uint32_t; using receiver_id = uint32_t; token remove(request_id req, receiver_id rec); token initiate_remove(receiver_id receiver) { auto req = new_request(); return remove(receiver, req); } My story begins

Slide 9

Slide 9 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 9/119 using request_id = uint32_t; using receiver_id = uint32_t; token remove(request_id req, receiver_id rec); token initiate_remove(receiver_id receiver) { auto req = new_request(); return remove(receiver, req); } My story begins

Slide 10

Slide 10 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 10/119 void other(A const& a); void func(B b) { other(b); } When is this call allowed?

Slide 11

Slide 11 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 11/119 struct A { int value; }; struct B { int value; }; void other(A const& a); void func(B b) { other(b); }

Slide 12

Slide 12 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 12/119 struct A { int value; }; struct B { int value; }; void other(A const& a); void func(B b) { other(b); } If we want this to compile, we can add:

Slide 13

Slide 13 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 13/119 struct A { int value; }; struct B { int value; }; void other(A const& a); void func(B b) { other(b); } If we want this to compile, we can add: A::A(B const&); // not explicit

Slide 14

Slide 14 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 14/119 struct A { int value; }; struct B { int value; }; void other(A const& a); void func(B b) { other(b); } If we want this to compile, we can add: A::A(B const&); // not explicit B::operator A(); // not explicit

Slide 15

Slide 15 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 15/119 struct A { int value; }; struct B { int value; }; void other(A const& a); void func(B b) { other(b); } If we want this to compile, we can add: A::A(B const&); // not explicit B::operator A(); // not explicit A as a public base class to B

Slide 16

Slide 16 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 16/119 struct request_id { uint32_t value; }; struct receiver_id { uint32_t value; }; token remove(request_id req, receiver_id rec); token initiate_remove(receiver_id receiver) { request_id req = new_request(); return remove(receiver, req); } A different story begins

Slide 17

Slide 17 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 17/119 struct request_id { uint32_t value; }; struct receiver_id { uint32_t value; }; token remove(request_id req, receiver_id rec); token initiate_remove(receiver_id receiver) { request_id req = new_request(); return remove(receiver, req); } error: no matching function for call to 'remove' return remove(receiver, req); ^~~~~~ note: candidate function not viable: no known conversion from 'receiver_id' to 'request_id' for 1st argument token remove(request_id req, receiver_id rec); ^

Slide 18

Slide 18 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 18/119 We have control over when the compiler will allow a conversion!

Slide 19

Slide 19 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 19/119 class receiver_id { public: explicit receiver_id(uint32_t v) : value{v} {} operator uint32_t() const { return value; } private: uint32_t value; };

Slide 20

Slide 20 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 20/119 class receiver_id { public: explicit receiver_id(uint32_t v) : value{v} {} operator uint32_t() const { return value; } bool operator==(receiver_id v) const { return value == v.value; } bool operator!=(receiver_id v) const; private: uint32_t value; };

Slide 21

Slide 21 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 21/119 class receiver_id { public: explicit receiver_id(uint32_t v) : value{v} {} operator uint32_t() const { return value; } bool operator==(receiver_id v) const { return value == v.value; } bool operator!=(receiver_id v) const; bool operator<(receiver_id v) const; ... private: uint32_t value; };

Slide 22

Slide 22 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 22/119 class receiver_id { public: explicit receiver_id(uint32_t v) : value{v} {} operator uint32_t() const { return value; } bool operator==(receiver_id v) const { return value == v.value; } bool operator!=(receiver_id v) const; bool operator<(receiver_id v) const; ... private: uint32_t value; }; There’s an awful lot of boiler plate code here!

Slide 23

Slide 23 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 23/119 class receiver_id { public: explicit receiver_id(uint32_t v) : value{v} {} operator uint32_t() const { return value; } bool operator==(receiver_id v) const { return value == v.value; } bool operator!=(receiver_id v) const; bool operator<(receiver_id v) const; ... private: uint32_t value; }; There’s an awful lot of boiler plate code here! Repeat once more for request_id

Slide 24

Slide 24 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 24/119 ● Introduction to type safety ● Type safety in C++ ● Simple library solution for strong types ● Sophisticated libraries – scouting github! ● What strong types does with your code Type Safe C++? - LOL! :-)

Slide 25

Slide 25 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 25/119 template class safe_type { public: private: T value_; };

Slide 26

Slide 26 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 26/119 template class safe_type { public: safe_type(T t) : value_(std::move(t)) {} operator T() const { return value_; } // operators... private: T value_; };

Slide 27

Slide 27 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 27/119 template class safe_type { public: safe_type(T t) : value_(std::move(t)) {} template safe_type(safe_type const&) = delete; operator T() const { return value_; } // operators... private: T value_; };

Slide 28

Slide 28 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 28/119 template class safe_type { public: safe_type(T t) : value_(std::move(t)) {} template safe_type(safe_type const&) = delete; operator T() const { return value_; } // operators... private: T value_; }; using int1 = safe_type; using int2 = safe_type;

Slide 29

Slide 29 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 29/119 using request_id = safe_type; using receiver_id = safe_type; token remove(request_id req, receiver_id rec); token initiate_remove(receiver_id receiver) { auto req = new_request(); return remove(receiver, req); }

Slide 30

Slide 30 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 30/119 using request_id = safe_type; using receiver_id = safe_type; token remove(request_id req, receiver_id rec); token initiate_remove(receiver_id receiver) { auto req = new_request(); return remove(receiver, req); }

Slide 31

Slide 31 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 31/119 using request_id = safe_type; using receiver_id = safe_type; token remove(request_id req, receiver_id rec); token initiate_remove(receiver_id receiver) { auto req = new_request(); return remove(receiver, req); }

Slide 32

Slide 32 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 32/119 using request_id = safe_type; using receiver_id = safe_type; token remove(request_id req, receiver_id rec); token initiate_remove(receiver_id receiver) { auto req = new_request(); return remove(receiver, req); } error: no matching function for call to 'remove' remove(receiver, req); ^~~~~~ note: candidate function not viable: no known conversion from 'safe_type<[...], struct receiver_id_tag>' to 'safe_type<[...], struct request_id_tag>' for 1st argument token remove(request_id req, receiver_id rec); ^

Slide 33

Slide 33 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 33/119 using request_id = safe_type; using receiver_id = safe_type; token remove(request_id req, receiver_id rec); token initiate_remove(receiver_id receiver) { auto req = new_request(); return remove(receiver, req); }

Slide 34

Slide 34 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 34/119 struct request_id : safe_type { using safe_type::safe_type; }; struct receiver_id : safe_type { using safe_type::safe_type; }; token remove(request_id req, receiver_id rec); token initiate_remove(receiver_id receiver) { auto req = new_request(); return remove(receiver, req); }

Slide 35

Slide 35 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 35/119 struct request_id : safe_type { using safe_type::safe_type; }; struct receiver_id : safe_type { using safe_type::safe_type; }; token remove(request_id req, receiver_id rec); token initiate_remove(receiver_id receiver) { auto req = new_request(); return remove(receiver, req); } error: no matching function for call to 'remove' remove(receiver, req); ^~~~~~ note: candidate function not viable: no known conversion from 'receiver_id' to 'request_id' for 1st argument token remove(request_id req, receiver_id rec); ^

Slide 36

Slide 36 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 36/119 struct request_id : safe_type { using safe_type::safe_type; }; struct receiver_id : safe_type { using safe_type::safe_type; }; token remove(request_id req, receiver_id rec); token initiate_remove(receiver_id receiver) { auto req = new_request(); return remove(receiver, req); } #define SAFE_TYPE(name, base_type) \ struct name : safe_type { \ using safe_type::safe_type; \ }

Slide 37

Slide 37 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 37/119 SAFE_TYPE(request_id, uint32_t); SAFE_TYPE(receiver_id, uint32_t); token remove(request_id req, receiver_id rec); token initiate_remove(receiver_id receiver) { auto req = new_request(); return remove(receiver, req); } #define SAFE_TYPE(name, base_type) \ struct name : safe_type { \ using safe_type::safe_type; \ }

Slide 38

Slide 38 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 38/119 ● Introduction to type safety ● Type safety in C++ ● Simple library solution for strong types ● Sophisticated libraries – scouting github! ● What strong types does with your code Type Safe C++? - LOL! :-)

Slide 39

Slide 39 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 39/119 Jonathan Müller @foonathan type_safe Zero overhead utilities for preventing bugs at compile time https://github.com/foonathan/type_safe

Slide 40

Slide 40 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 40/119 Jonathan Müller @foonathan type_safe Zero overhead utilities for preventing bugs at compile time https://github.com/foonathan/type_safe A rich type library, with which you can piece together the exact behaviour of a type that you want. It also includes a number of predefined neat type templates, and other features like improved optional and variant

Slide 41

Slide 41 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 41/119 // type_safe/strong_typedef.hpp template class type_safe::strong_typedef { public: constexpr strong_typedef(); explicit constexpr strong_typedef(const T& value); explicit constexpr strong_typedef(T&& value); explicit constexpr operator T&() & noexcept; explicit constexpr operator const T&() const & noexcept; explicit constexpr operator T&&() && noexcept; explicit constexpr operator const T&&() const && noexcept; }; https://github.com/foonathan/type_safe

Slide 42

Slide 42 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 42/119 https://github.com/foonathan/type_safe #include

Slide 43

Slide 43 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 43/119 https://github.com/foonathan/type_safe #include namespace ts = type_safe;

Slide 44

Slide 44 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 44/119 https://github.com/foonathan/type_safe #include namespace ts = type_safe; struct my_handle : ts::strong_typedef { using strong_typedef::strong_typedef; };

Slide 45

Slide 45 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 45/119 https://github.com/foonathan/type_safe #include namespace ts = type_safe; namespace op = type_safe::strong_typedef_op; struct my_handle : ts::strong_typedef { using strong_typedef::strong_typedef; };

Slide 46

Slide 46 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 46/119 https://github.com/foonathan/type_safe #include namespace ts = type_safe; namespace op = type_safe::strong_typedef_op; struct my_handle : ts::strong_typedef , op::equality_comparison { using strong_typedef::strong_typedef; };

Slide 47

Slide 47 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 47/119 https://github.com/foonathan/type_safe #include namespace ts = type_safe; namespace op = type_safe::strong_typedef_op; struct my_handle : ts::strong_typedef , op::equality_comparison , op::output_operator { using strong_typedef::strong_typedef; };

Slide 48

Slide 48 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 48/119 https://github.com/foonathan/type_safe #include namespace ts = type_safe; namespace op = type_safe::strong_typedef_op; struct my_handle : ts::strong_typedef , op::equality_comparison , op::output_operator { using strong_typedef::strong_typedef; }; struct my_int : ts::strong_typedef , op::integer_arithmetic { using strong_typedef::strong_typedef; };

Slide 49

Slide 49 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 49/119 https://github.com/foonathan/type_safe #include namespace ts = type_safe; namespace op = type_safe::strong_typedef_op; struct my_handle : ts::strong_typedef , op::equality_comparison { using strong_typedef::strong_typedef; friend std::ostream& operator<<(std::ostream& os, my_handle const& h) { return os << "H{" << static_cast(h) << "}"; } };

Slide 50

Slide 50 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 50/119 https://github.com/foonathan/type_safe #include namespace ts = type_safe; namespace op = type_safe::strong_typedef_op; struct my_handle : ts::strong_typedef , op::equality_comparison { using strong_typedef::strong_typedef; friend std::ostream& operator<<(std::ostream& os, my_handle const& h) { return os << "H{" << ts::get(h) << "}"; } };

Slide 51

Slide 51 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 51/119 Jonathan Boccara @joboccara NamedType Implementation of strong types in C++ https://github.com/joboccara/NamedType

Slide 52

Slide 52 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 52/119 Jonathan Boccara @joboccara NamedType Implementation of strong types in C++ https://github.com/joboccara/NamedType A small type library with a simpler aim, but which still allows you to piece together the strong types with your desired behaviour. It also supports conversions between different types of the same kind, for example meters to feet, or non-linear like Watt to dB.

Slide 53

Slide 53 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 53/119 Jonathan Boccara @joboccara NamedType Implementation of strong types in C++ https://github.com/joboccara/NamedType A small type library with a simpler aim, but which still allows you to piece together the strong types with your desired behaviour. It also supports conversions between different types of the same kind, for example meters to feet, or non-linear like Watt to dB. MeetingC++ https://www.youtube.com/watch?v=WVleZqzTw2k

Slide 54

Slide 54 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 54/119 #include using my_handle = fluent::NamedType< int, struct my_handle_tag >; https://github.com/joboccara/NamedType

Slide 55

Slide 55 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 55/119 #include using my_handle = fluent::NamedType< int, struct my_handle_tag, fluent::comparable, fluent::printable, fluent::hashable >; https://github.com/joboccara/NamedType

Slide 56

Slide 56 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 56/119 #include struct my_handle : fluent::NamedType< int, my_handle, fluent::comparable, fluent::printable, fluent::hashable > { using NamedType::NamedType; }; https://github.com/joboccara/NamedType

Slide 57

Slide 57 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 57/119 #include struct my_handle : fluent::NamedType< int, my_handle, fluent::comparable, fluent::printable, fluent::hashable, fluent::ImplicitlyConvertibleTo::templ > { using NamedType::NamedType; }; https://github.com/joboccara/NamedType

Slide 58

Slide 58 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 58/119 ● Introduction to type safety ● Type safety in C++ ● Simple library solution for strong types ● Sophisticated libraries – scouting github! ● What strong types does with your code Type Safe C++? - LOL! :-)

Slide 59

Slide 59 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 59/119 Network capacity utilisation 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots A slot is a network capacity quanta

Slide 60

Slide 60 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 60/119 Network capacity utilisation 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots SlotCount SlotIndexes SlotRanges 8 3,4,7,8,13,14,15,16 {3-4},{7-8},{13-16} 3 5,9,10 {5},{9-10} 13 0,1,2,6,11,12,17,18, 19,20,21,22,23 {0-2},{6},{11-12},{17-23} A slot is a network capacity quanta

Slide 61

Slide 61 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 61/119 Network capacity utilisation 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots SlotCount SlotIndexes SlotRanges 8 3,4,7,8,13,14,15,16 {3-4},{7-8},{13-16} 3 5,9,10 {5},{9-10} 13 0,1,2,6,11,12,17,18, 19,20,21,22,23 {0-2},{6},{11-12},{17-23} A slot is a network capacity quanta typename SlotIndex; typename SlotCount; struct SlotRange{ SlotIndex start; SlotCount length; };

Slide 62

Slide 62 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 62/119 Magic Numbers

Slide 63

Slide 63 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 63/119 Connection createConnection(Address destination, SlotCount capacity); Connection createSDI_Connection(Address destination) { return createConnection(destination, 528); }

Slide 64

Slide 64 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 64/119 Connection createConnection(Address destination, SlotCount capacity); Connection createSDI_Connection(Address destination) { return createConnection(destination, SlotCount{528}); }

Slide 65

Slide 65 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 65/119 Connection createConnection(Address destination, SlotCount capacity); constexpr SlotCount SDI_Capacity{528}; Connection createSDI_Connection(Address destination) { return createConnection(destination, SDI_Capacity); }

Slide 66

Slide 66 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 66/119 Encapsulation

Slide 67

Slide 67 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 67/119 class MessageBuffer { public: template void serialize_bits(unsigned value); }; SlotCount capacity = ... MessageBuffer buffer ... buffer.serialize_bits<24>(capacity);

Slide 68

Slide 68 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 68/119 class MessageBuffer { public: template void serialize_bits(unsigned value); }; SlotCount capacity = ... MessageBuffer buffer ... buffer.serialize_bits<24>(capacity);

Slide 69

Slide 69 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 69/119 class MessageBuffer { public: template void serialize_bits(unsigned value); }; void serialize_data(MessageBuffer& b, SlotCount const& c) { b.serialize_bits<24>(c); } SlotCount capacity = ... MessageBuffer buffer ... serialize_data(buffer, capacity);

Slide 70

Slide 70 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 70/119 class MessageBuffer { public: template void serialize(T const& t) { serialize_data(*this, t); } template void serialize_bits(unsigned value); }; void serialize_data(MessageBuffer& b, SlotCount const& c) { b.serialize_bits<24>(c); } SlotCount capacity = ... MessageBuffer buffer ... buffer.serialize(capacity);

Slide 71

Slide 71 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 71/119 Type Semantics

Slide 72

Slide 72 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 72/119 class SlotPool { public: void releaseCapacity(std::vector const& ranges) SlotCount availableCapacity() const { return unusedSlots;} ... private: SlotCount unusedSlots; ... };

Slide 73

Slide 73 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 73/119 class SlotPool { public: void releaseCapacity(std::vector const& ranges) { for (auto& range : ranges) { unusedSlots += range.length; } ... } SlotCount availableCapacity() const { return unusedSlots;} ... private: SlotCount unusedSlots; ... };

Slide 74

Slide 74 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 74/119 class SlotPool { public: void releaseCapacity(std::vector const& ranges) { for (auto& range : ranges) { unusedSlots += range.length; } ... } SlotCount availableCapacity() const { return unusedSlots;} ... private: SlotCount unusedSlots; ... }; Does not compile! No operator += for SlotCount

Slide 75

Slide 75 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 75/119 Which operations makes sense? SlotCount+SlotCount? 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 76

Slide 76 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 76/119 Which operations makes sense? SlotCount+SlotCount->SlotCount 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 77

Slide 77 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 77/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount? 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 78

Slide 78 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 78/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 79

Slide 79 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 79/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount? 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 80

Slide 80 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 80/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 81

Slide 81 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 81/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio? 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 82

Slide 82 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 82/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 83

Slide 83 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 83/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount? 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 84

Slide 84 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 84/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 85

Slide 85 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 85/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio? 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 86

Slide 86 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 86/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 87

Slide 87 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 87/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex? 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 88

Slide 88 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 88/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 89

Slide 89 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 89/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount? 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 90

Slide 90 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 90/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount->SlotIndex 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 91

Slide 91 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 91/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount->SlotIndex SlotIndex-SlotIndex? 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 92

Slide 92 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 92/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount->SlotIndex SlotIndex-SlotIndex->SlotCount 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 93

Slide 93 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 93/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount->SlotIndex SlotIndex-SlotIndex->SlotCount SlotIndex/SlotIndex? 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 94

Slide 94 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 94/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount->SlotIndex SlotIndex-SlotIndex->SlotCount SlotIndex/SlotIndex 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 95

Slide 95 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 95/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount->SlotIndex SlotIndex-SlotIndex->SlotCount SlotIndex/SlotIndex SlotIndex/SlotCount? 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 96

Slide 96 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 96/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount->SlotIndex SlotIndex-SlotIndex->SlotCount SlotIndex/SlotIndex SlotIndex/SlotCount 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 97

Slide 97 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 97/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount->SlotIndex SlotIndex-SlotIndex->SlotCount SlotIndex/SlotIndex SlotIndex/SlotCount SlotIndex/Ratio? 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 98

Slide 98 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 98/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount->SlotIndex SlotIndex-SlotIndex->SlotCount SlotIndex/SlotIndex SlotIndex/SlotCount SlotIndex/Ratio 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 99

Slide 99 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 99/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount->SlotIndex SlotIndex-SlotIndex->SlotCount SlotIndex/SlotIndex SlotIndex/SlotCount SlotIndex/Ratio SlotIndex*SlotIndex? 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 100

Slide 100 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 100/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount->SlotIndex SlotIndex-SlotIndex->SlotCount SlotIndex/SlotIndex SlotIndex/SlotCount SlotIndex/Ratio SlotIndex*SlotIndex 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 101

Slide 101 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 101/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount->SlotIndex SlotIndex-SlotIndex->SlotCount SlotIndex/SlotIndex SlotIndex/SlotCount SlotIndex/Ratio SlotIndex*SlotIndex SlotIndex*SlotCount? 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 102

Slide 102 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 102/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount->SlotIndex SlotIndex-SlotIndex->SlotCount SlotIndex/SlotIndex SlotIndex/SlotCount SlotIndex/Ratio SlotIndex*SlotIndex SlotIndex*SlotCount 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 103

Slide 103 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 103/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount->SlotIndex SlotIndex-SlotIndex->SlotCount SlotIndex/SlotIndex SlotIndex/SlotCount SlotIndex/Ratio SlotIndex*SlotIndex SlotIndex*SlotCount SlotIndex*Ratio? 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 104

Slide 104 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 104/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount->SlotIndex SlotIndex-SlotIndex->SlotCount SlotIndex/SlotIndex SlotIndex/SlotCount SlotIndex/Ratio SlotIndex*SlotIndex SlotIndex*SlotCount SlotIndex*Ratio 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots

Slide 105

Slide 105 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 105/119 Which operations makes sense? SlotCount+SlotCount->SlotCount SlotCount-SlotCount->SlotCount SlotCount*SlotCount SlotCount*Ratio->SlotCount SlotCount/SlotCount->Ratio SlotCount/Ratio->SlotCount SlotIndex+SlotIndex SlotIndex+SlotCount->SlotIndex SlotIndex-SlotIndex->SlotCount SlotIndex/SlotIndex SlotIndex/SlotCount SlotIndex/Ratio SlotIndex*SlotIndex SlotIndex*SlotCount SlotIndex*Ratio 0 1 2 3 4 5 6 7 8 9 10 11 121314151617181920212223 Frame with 24 slots Affine Space In mathematics, an affine space is a geometric structure that generalizes the properties of Euclidean spaces in such a way that these are independent of the concepts of distance and measure of angles, keeping only the properties related to parallelism and ratio of lengths for parallel line segments... -- Wikipedia

Slide 106

Slide 106 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 106/119 Test Code

Slide 107

Slide 107 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 107/119 DestClient::newCapacity(RequestId, SlotCount); TestNode::throttleCapacityTo(RequestId, SlotCount total); TEST(capacity_decrease_is_notified_to_clients) { TestNode node; DestClient client1 = node.clientWithCapacity(5); DestClient client2 = node.clientWithCapacity(8); REQUIRE_CALL(client1, newCapacity(4, 2)); REQUIRE_CALL(client2, newCapacity(4, 3)); node.throttleCapacityTo(4, 5); }

Slide 108

Slide 108 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 108/119 DestClient::newCapacity(RequestId, SlotCount); TestNode::throttleCapacityTo(RequestId, SlotCount total); TEST(capacity_decrease_is_notified_to_clients) { TestNode node; DestClient client1 = node.clientWithCapacity(5); DestClient client2 = node.clientWithCapacity(8); RequestId req{4}; REQUIRE_CALL(client1, newCapacity(req, 2)); REQUIRE_CALL(client2, newCapacity(req, 3)); node.throttleCapacityTo(req, 5); }

Slide 109

Slide 109 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 109/119 DestClient::newCapacity(RequestId, SlotCount); TestNode::throttleCapacityTo(RequestId, SlotCount total); TEST(capacity_decrease_is_notified_to_clients) { TestNode node; SlotCount c1Capacity{5}, c2Capacity{8}; DestClient client1 = node.clientWithCapacity(c1Capacity); DestClient client2 = node.clientWithCapacity(c2Capacity); RequestId req{4}; SlotCount newC1Capacity{2}, newC2Capacity{3}; REQUIRE_CALL(client1, newCapacity(req, newC1Capacity)); REQUIRE_CALL(client2, newCapacity(req, newC2Capacity)); SlotCount newTotalCapacity{5}; node.throttleCapacityTo(req, newTotalCapacity); }

Slide 110

Slide 110 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 110/119 DestClient::newCapacity(RequestId, SlotCount); TestNode::throttleCapacityTo(RequestId, SlotCount total); TEST(capacity_decrease_is_notified_to_clients) { TestNode node; SlotCount c1Capacity{5}, c2Capacity{8}; DestClient client1 = node.clientWithCapacity(c1Capacity); DestClient client2 = node.clientWithCapacity(c2Capacity); RequestId req{4}; SlotCount newC1Capacity{2}, newC2Capacity{3}; REQUIRE_CALL(client1, newCapacity(req, newC1Capacity)); REQUIRE_CALL(client2, newCapacity(req, newC2Capacity)); SlotCount newTotalCapacity{5}; node.throttleCapacityTo(req, newTotalCapacity); } WTF!

Slide 111

Slide 111 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 111/119 DestClient::newCapacity(RequestId, SlotCount); TestNode::throttleCapacityTo(RequestId, SlotCount total); TEST(capacity_decrease_is_notified_to_clients) { TestNode node; DestClient client1 = node.clientWithCapacity(SlotCount{5}); DestClient client2 = node.clientWithCapacity(SlotCount{8}); RequestId req{4}; REQUIRE_CALL(client1, newCapacity(req, SlotCount{2})); REQUIRE_CALL(client2, newCapacity(req, SlotCount{3})); node.throttleCapacityTo(req, SlotCount{5}); }

Slide 112

Slide 112 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 112/119 ● Introduction to type safety ● Type safety in C++ ● Simple library solution for strong types ● Sophisticated libraries – scouting github! ● What strong types does with your code Type Safe C++? - LOL! :-)

Slide 113

Slide 113 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 113/119 ● Safety for built in types is abysmal in C++ Type Safe C++? - LOL! :-)

Slide 114

Slide 114 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 114/119 ● Safety for built in types is abysmal in C++ ● Structs/classes are as strong as you wish – You must add the functionality you want Type Safe C++? - LOL! :-)

Slide 115

Slide 115 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 115/119 ● Safety for built in types is abysmal in C++ ● Structs/classes are as strong as you wish – You must add the functionality you want ● Libraries exist that makes this easier Type Safe C++? - LOL! :-)

Slide 116

Slide 116 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 116/119 ● Safety for built in types is abysmal in C++ ● Structs/classes are as strong as you wish – You must add the functionality you want ● Libraries exist that makes this easier ● Thinking about what operations your types should support makes you understand the problem better! – Beware of the urge for convencience! Type Safe C++? - LOL! :-)

Slide 117

Slide 117 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 117/119 ● Safety for built in types is abysmal in C++ ● Structs/classes are as strong as you wish – You must add the functionality you want ● Libraries exist that makes this easier ● Thinking about what operations your types should support makes you understand the problem better! – Beware of the urge for convencience! ● Strong types leads to more expressive code – Fewer magical numbers – More encapsulation – Explicit tests that express intent Type Safe C++? - LOL! :-)

Slide 118

Slide 118 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 118/119 ● Safety for built in types is abysmal in C++ ● Structs/classes are as strong as you wish – You must add the functionality you want ● Libraries exist that makes this easier ● Thinking about what operations your types should support makes you understand the problem better! – Beware of the urge for convencience! ● Strong types leads to more expressive code – Fewer magical numbers – More encapsulation – Explicit tests that express intent Type Safe C++? - LOL! :-) Avoid typedef

Slide 119

Slide 119 text

Type Safe C++? - LOL! :-) – NDC{Techtown} 2018 – © Björn Fahller @bjorn_fahller 119/119 Björn Fahller bjorn@fahller.se @bjorn_fahller @rollbear cpplang, swedencpp Type Safe C++? - LOL! :-) https://github.com/rollbear/strong_type