TDDD38/726G82 - Advanced programming in ++ Sum Types in C++ Christoffer Holm

Department of Computer and informaon science 1 Intro 2 Union 3 STL types 4 Implementaon 5 Second Implementaon 1 Intro 2 Union 3 STL types 4 Implementaon 5 Second Implementaon ‚ Can we simulate dynamic typing though?

3 / 66

Intro

Goals

‚ C++ is stacally typed 3 / 66

Intro

Goals

‚ C++ is stacally typed ‚ Can we simulate dynamic typing though? ‚ Sum types

4 / 66

Intro

Type categories

Algebraic Data Types ‚ Product types ‚ Sum types

4 / 66

Intro

Type categories

Algebraic Data Types ‚ Product types ‚ A type containing several other types at once

‚ struct and class types are product types

‚ std::pair and std:: 4 / 66

Intro

Type categories

Algebraic Data Types ‚ Product types ‚ Sum types 4 / 66

Intro

Type categories

Algebraic Data Types ‚ Product types ‚ Sum types ‚ A sum type is a type that can take on one of several types at a me ‚ I.e. a type which can only store one value, but that value might be chosen from more than one type 5 / 66

Intro

Product Type

n int

c char Type 6 / 66

Intro

Sum Type

int

value char Type

char const* 6 / 66

Intro

Sum Type

int

value char Type

char const*

value : 5 6 / 66

Intro

Sum Type

int

value char Type

char const*

value : 'a' 6 / 66

Intro

Sum Type

int

value char Type

char const*

value : "some text" ‚ but how do they work in C++?

7 / 66

Intro

This sounds like Python (sort of)

‚ use sum types to simulate dynamic types 7 / 66

Intro

This sounds like Python (sort of)

‚ use sum types to simulate dynamic types ‚ but how do they work in C++? 1 Intro 2 Union 3 STL types 4 Implementaon 5 Second Implementaon 9 / 66

Union

Unions

int main() union Sum_Type { { Sum_Type obj; int n; obj.n = 5; char c; obj.c = 'a'; char const* s; obj.s = "some text"; }; } 10 / 66

Union

Unions

‚ Unions look like struct or class ‚ They work very different though ‚ Only one field can be at one point 11 / 66

Union

Problems with unions

int main() { Sum_Type obj; obj.n = 5; cout << obj.c << endl; } 11 / 66

Union

Problems with unions

int main()Undefined Behaviour { Sum_Type obj; obj.n = 5; cout << obj.c << endl; } 12 / 66

Union

Problems with unions

‚ The only field that is safe to access is the one last set ‚ Accessing any other field will be undefined behaviour ‚ Once we assign to a new field the old one will be overwrien 13 / 66

Union

(Possible) Memory model of unions

union Sum_Type { int n; // 4 char c; // 1 char const* s; // 8 bytes };

sizeof(Sum_Type) == 8 14 / 66

Union

(Possible) Memory model of unions

Sum_Type obj; 14 / 66

Union

(Possible) Memory model of unions

s

obj.s = "some text"; 14 / 66

Union

(Possible) Memory model of unions

0 x 4 0 0 8 5

s

obj.s = "some text"; 14 / 66

Union

(Possible) Memory model of unions

0 x 4 0 0 8 d 5

n

obj.n = 5; 14 / 66

Union

(Possible) Memory model of unions

0 0 0 5 0 8 d 5

n

obj.n = 5; 14 / 66

Union

(Possible) Memory model of unions

0 0 0 5 0 8 d 5

c

obj.c = 'a'; 14 / 66

Union

(Possible) Memory model of unions

a 0 0 5 0 8 d 5

c

obj.c = 'a'; 14 / 66

Union

(Possible) Me