0% found this document useful (0 votes)
4 views71 pages

Section LiteralsAndConstants Slides

The document discusses literals and constants in C++, highlighting that literals are data directly represented in code, while constants are variables that cannot be changed after initialization. It introduces key concepts such as 'const', 'constexpr', and 'constinit', explaining their roles in ensuring data integrity and compile-time evaluation. The presentation emphasizes the importance of using 'const' for self-documenting code and compiler protection against unintended modifications.

Uploaded by

Strix 621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views71 pages

Section LiteralsAndConstants Slides

The document discusses literals and constants in C++, highlighting that literals are data directly represented in code, while constants are variables that cannot be changed after initialization. It introduces key concepts such as 'const', 'constexpr', and 'constinit', explaining their roles in ensuring data integrity and compile-time evaluation. The presentation emphasizes the importance of using 'const' for self-documenting code and compiler protection against unintended modifications.

Uploaded by

Strix 621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Slides

Section :Literals and


constants
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
Literals and Constants :
Introduction

3
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Data that is directly represented in
Literal code without going through some
other variable stored in memory

4
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
They are literally stored in the
Literal program executable file, hence the
name literal!

5
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
A read only variable. Can’t assign
const
data to it

6
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
A constant that has the POTENTIAL to
constexpr
be evaluated at compile time

7
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
A variable that should be initialized
constinit
with a constant or literal at compile
time

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

9
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Literals

10
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Data that is directly represented in
Literal code without going through some
other variable stored in memory

11
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
They are literally stored in the
Literal program executable file, hence the
name literal!

12
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
13
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
a= 10 (int)
CPU
b = 5 (int)
c (int)
print(“Statement1”)
Program print(“Statement2”)
c = f_add(a,b)
area print(“Statement3”)
print(“Statement4”)
end

Hard Drive

10 a
5 b

param1
14
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
param2
Param1 + param2
15
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
16
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
17
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
18
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
19
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
20
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
21
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

22
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Constants

23
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
A variable you can initialize, but
Constant
can’t change afterwards

24
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
25
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
26
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
The const qualifier applies to the variable name you
are using to manipulate data in memory. It doesn’t
apply to the 0’s and 1’s in memory themselves

27
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
28
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
29
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
30
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Why const ?

31
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Guarantee that the variable will never change
throughout the lifetime of your program

32
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
33
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
34
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
35
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
36
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
int const int

37
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
When to use const ?

38
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• Using const where it makes sense in your code makes it self documenting in that when
someone sees your variable declaration, they instantly know that it is a read only piece of data

• You also get the compiler protection when you try to modify the read only variable by mistake

• It is recommended to make your variables const where possible

• I personally declare most of my variables const and then take the const out when I need the
variable to be modified.

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

40
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Constant Expressions

41
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
IDE

Compile Time
Compiler

Run Time Executable binary file


42
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
constexpr Constant that may be evaluated at compile time or runtime

43
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• If possible, move the potentially heavy computations at compile time

• The heavy computation is done once by the developer and users running the
application can benefit from the results of the computation done at compile
time

44
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Compiler

Executable binary file


45
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Initialization

Constexpr
Executable binary file
Computation

Runtime
computations

46
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Initialization Initialization Initialization Initialization

Constexpr Constexpr Constexpr Constexpr


Computation Computation Computation Computation

Runtime Runtime Runtime Runtime


computations computations computations computations

47
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Constexpr
Compiler Computation

Executable binary file


48
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Initialization

Executable binary file


Runtime
computations

49
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Initialization Initialization Initialization Initialization

Runtime Runtime Runtime Runtime


computations computations computations computations

50
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Constant expressions have been introduced in C++11
Before that, everything was done at runtime
With every new iteration of the C++ standard, we see the range of things we can
do at compile time increase.

51
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Runtime computations

52
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Compile time computations

Runtime computations

53
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Compile time computations

Runtime computations

54
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Compile time computations

Runtime computations

55
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Compile time computations

Runtime computations

56
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Compile time computations

Runtime computations

57
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
58
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Checks at compile time

59
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Constant expressions are also constants, so
you can’t reassign values to them

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

61
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
constinit

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

65
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Section Summary

66
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Literals and constants

67
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Data that is directly represented in
Literal code without going through some
other variable stored in memory

68
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
A variable you can initialize, but
Constant
can’t change afterwards

69
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
constexpr static_assert()

constinit

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

You might also like