Section Cpp20 Modules Slides
Section Cpp20 Modules Slides
Section Cpp20 Modules Slides
2
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
C++20 Modules
3
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Concepts Ranges
Coroutines Modules
4
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• One of the big C++20 features
• Changes the compilation model of C++ programs
5
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
math.h
main.cpp
#include “math.h”
math.cpp
#include “math.h”
stuff.cpp
#include “math.h”
6
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Problems with headers
• Compilation speed
• ODR violations
• Include order
• …
7
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
8
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
math.ixx
main.cpp
import math;
math.cpp
import math;
stuff.cpp
import math;
9
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
main.cpp
import math;
math.ixx BMI
math.cpp
import math;
math
stuff.cpp
import math;
10
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
With modules
11
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
#include <cstring>; Global Module Fragment
Module preamble
Module purview
12
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Global Module fragment
13
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Module preamble
14
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Module purview
15
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Facts
16
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Environment
• Windows 10
• Miscrosoft Visual Studio 2019 16.9.0 Preview 2
17
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Compiler Build system IDE
18
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
19
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Your First Module
20
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
math.ixx
21
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
main.cpp
22
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Module interface file
• If the module declaration has export in front of it, the file containing the
module becomes a module interface file
23
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Module interface file extension
msvc : .ixx
Gcc/Clang : .cc/.cppm
* gcc : https://gcc.gnu.org/wiki/cxx-modules
24
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Three options for working with modules
• Include translation
• Header importation
• Module importation
25
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Include translation
• The compiler actually goes in and transforms the header into a legit
module interface file by inserting proper module declarations and
export statements where it makes sense
27
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Module importation
28
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Module importation
The name you use in your import statement is not based on the name
of the file containing the module, it’s based on the name in your
module declaration statement
29
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
30
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
31
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Block Export
32
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
math.ixx
33
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
math.ixx
34
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
35
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Separating the interface from the
implementation
36
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Interface
37
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Implementation
38
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
39
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Separating the interface from the
implementation : Different files
40
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Interface : lives in module interface file (.ixx)
41
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Module implementation file
42
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
43
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Multiple Module Implementation
Files
44
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Interface : lives in module interface file (.ixx)
45
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
math.cpp
46
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
print.cpp
47
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
48
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Multiple Interface Files
49
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
math.ixx (OLD)
50
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
math.ixx
51
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
print.ixx
52
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
math.cpp
53
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
print.cpp
54
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
55
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Export Import
56
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
print.ixx
57
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
main.cpp
58
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
59
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Sub-modules
60
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
math
math.add_sub math.mult_div
61
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
62
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
main.cpp
63
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
64
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Module interface partitions
65
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
math
math:addition math:multiplication
66
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
67
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
main.cpp
68
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
More on Modules
69
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
References
https://vector-of-bool.github.io/2019/03/10/modules-1.html
70
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
71
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
C++20 Modules : Summary
72
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• One of the big C++20 features
• Changes the compilation model of C++ programs
73
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
main.cpp
import math;
math.ixx BMI
math.cpp
import math;
math
stuff.cpp
import math;
74
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
#include <cstring>; Global Module Fragment
Module preamble
Module purview
75
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Block export
76
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Separate Interface from Implementation – Same File/ Different files
77
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Multiple implementation files for an interface
78
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Multiple Interffaces
79
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Export Import
80
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Submodules
81
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Module Interface
Partitions
82
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty
83
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya