Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Section Cpp20 Modules Slides

Download as pdf or txt
Download as pdf or txt
You are on page 1of 83

Slides

Section : C++20 Modules


1
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

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

• Compilation speed improved by BMI


• ODR violations. None. BMI lives in one object file
• Include order. Doesn’t matter. Modules available in
some pre-compiled form in the BMI file
• …

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

• Can only contain preprocessor directives


• Allows old style #include’ing of headers in a module
• #include statements can only show up in the global module
fragment, if you put them elsewhere, you’ll get weird errors

13
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Module preamble

• Should only contain import statements. May be importing


proper module files, or just plain old non C-library headers

14
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Module purview

• Contains the meat of the module; functions, types ,… that


make up the interface of the module
• Some entities are exported, making them visible outside the
module
• Those not exported are only usable inside the module itself

15
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Facts

• Each module is compiled independently into an object file


• Modules must be compiled in an order in such a way that all
the dependencies are compiled first.
• If you are compiling manually on the command line yourself,
you have to take care of this order yourself
• In practice however, IDEs and build systems will figure out this
order automatically.

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

• The collection of entities export’ed from a module file make up the


module interface

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

• Plain old raw #include statements in the global module fragment

• Other preprocessing directives should show up in the global module


fragment too

• Headers that originate from the C Standard Library like <cmath>,


<cstring>, … can only shop up in the global module fragment. You’ll
regognize them because their names are prefixed with a “c”

• The magic by which non modular code in the global module


fragment is converted and consumed in our module is out of scope
for this course
26
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Header Importation

• Imports in the module preamble like import <iostream>;

• They are ended by a semicolon “;”

• Headers imported with the syntax “import <iostream>;” are called


header units

• 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

• Module Implementation Partitions


• Visiblility and Reachability
• …

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

You might also like