SlideShare a Scribd company logo
1 of 124
Regular types
Ilio Catallo - info@iliocatallo.it
Outline
• Fundamental opera/ons
• Regular types
• Semi-regular & totally ordered types
• Making vector_int regular
• Bibliography
Fundamental opera.ons
Commonali(es among types
What is in common between chars and doubles1
?
char c1 = 'a', c2 = 'b';
double d1 = 1.3, d2 = 2.7;
1
For the remainder of this presenta1on, we will limit ourselves to non-singular double values, i.e., we will exclude
NaN from the set of possible values
Commonali(es among types
For a start, both chars and doubles and can be swapped
std::swap(c1, c2);
std::swap(d1, d2);
Why is that so?
swap implementa*on for doubles
Let us look at the implementa0on of swap as if it was wri4en just
for doubles
void swap(double& x, double& y) {
double tmp = x;
x = y;
y = tmp;
}
Requirements on doubles
Which requirements on doubles are needed for swap to work?
void swap(double& x, double& y) {
double tmp = x;
x = y;
y = tmp;
}
Requirements on doubles
First, it should be possible to copy construct a double
void swap(double& x, double& y) {
double tmp = x; // tmp is copy constructed from x
x = y;
y = tmp;
}
Requirements on doubles
Second, it should be possible to assign a double
void swap(double& x, double& y) {
double tmp = x;
x = y; // x is assigned y
y = tmp; // y is assigned tmp
}
Requirements on doubles
Given that double values are in fact both assignable and copy
construc1ble, we can run swap on them
std::swap(d1, d2);
swap implementa*on for chars
Let us now turn our a,en-on to a version of swap tailored for
chars
void swap(char& x, char& y) {
char tmp = x;
x = y;
y = tmp;
}
Requirements on chars
As before, we no,ce that char values need to be assignable and
copy construc1ble
void swap(char& x, char& y) {
char tmp = x;
x = y;
y = tmp;
}
Requirements on chars
As before, since char values are in fact assignable and copy
construc1ble, it is possible to swap them
std::swap(c1, c2);
Swapping built-in types
It is immediate to see that our finding generalizes to all built-in
types
std::swap(b1, b2); // b1 and b2 are bools
std::swap(f1, f2); // f1 and f2 are floats
std::swap(i1, i2); // i1 and i2 are ints
Common opera*ons
We conclude that assignment and copy construc0on are
opera/ons common to all built-in types
Common opera*ons
That is, for any built-in type T the following is always possible:
T a; a = b; // assignment
T c = d; // copy construction
Common opera*ons
In light of this, we can write a uniform implementa.on of swap for
all built-in types
void swap(T& x, T& y) { // T is a built-in type
T tmp = x;
x = y;
y = tmp;
}
Do built-in types share any addi3onal opera3ons?
Fundamental opera.ons
As a ma&er of fact, we observe that there is a core set of
opera4ons which are defined for all built-in types
Fundamental opera.ons
We call such opera-ons the fundamental opera.ons
┌────────────────────────┬──────────┐
│ Default construction │ T a; │
├────────────────────────┼──────────┤
│ Copy construction │ T a = b; │
├────────────────────────┼──────────┤
│ Destruction │ ~T(a); │
├────────────────────────┼──────────┤
│ Assignment │ a = b; │
├────────────────────────┼──────────┤
│ Equality │ a == b; │
├────────────────────────┼──────────┤
│ Inequality │ a != b; │
├────────────────────────┼──────────┤
│ Ordering │ a < b; │
└────────────────────────┴──────────┘
Regular types
Operators for UDTs
The C++ programming language allows the use of built-in type
operator syntax for user-defined types (UDTs)
Example: the point2
UDT
struct point {
point(double x, double y): x(x), y(y) {}
double x;
double y;
};
2
Remember that there is no difference between a struct and a class, up to default members' visibility.
Conven=onally, we use structs when our UDTs do not need a private sec=on
Example: the point UDT
Intui&vely, it makes sense to define operator == as equality:
bool operator==(point const& p1, point const& p2) {
return p1.x == p2.x &&
p1.y == p2.y;
}
Overload seman-cs
However, C++ does not dictate any seman&c rules on operator
overloading
Overload seman-cs
That is, developers are free to define == as:
bool operator==(point const& p1, point const& p2) {
return p1.x * p2.x >
p1.y * p2.y;
}
Seman&cs mismatch
This could lead to a mismatch between the expected and the
actual seman4cs of an operator
Seman&cs mismatch
This could lead to a mismatch between the expected and the
actual seman4cs of an operator
( °□°
Seman&cs mismatch
"I admire the commi.ee dedica/on to programming freedom, but I
claim that such freedom is an illusion. Finding the laws that govern
so=ware components gives us freedom to write complex programs the
same way that finding the laws of physics allows us to construct
complex mechanical and electrical systems."
(A. Stepanov)
Built-in types as a theore1cal founda1on
The opera)ons common to all built-in types are among the most
well-understood and pervasively used
Built-in types as a theore1cal founda1on
This is because, over the years, the development of built-in types
has led to consistent defini9ons that match
• the programmer intui-on, and
• the underlying mathema-cal understanding
Idea: deriving the seman.cs of user-defined types from that of
built-in types
Preserving opera-on seman-cs
The idea is that when a code fragment has a certain meaning for
built-in types, it should preserve the same meaning for UDTs
Regular types
To this end, we introduce the concept of a regular type as a type
behaving like the built-in types
Regular types
We call such types regular, since their use guarantees regularity of
behavior and – therefore – interoperability
std::vector is regular
For instance, std::vector<T> is regular whenever T is regular
std::vector is regular
As with built-in types, std::vector<T> is thus copy
construc+ble and assignable for each regular type T
std::vector<int> w = v; // copy construction
std::vector<int> u; u = w; // assignment
std::vector is swappable
Hence, we can swap3
two vectors of any regular type T:
std::vector<int> v = {7, 3, 5};
std::vector<int> w = {10, 11, 4};
std::swap(v, w);
3
In reality, for the sake of performance, the Standard Library provides a specialized version std::swap for the
specific case of std::vector. Although less performing, the general purpose version would however equally work
How can we assure our UDTs to be regular?
What does it mean to be regular?
Ul#mately, we need to inves#gate how several of the built-in
operators should behave when applied to user-defined types
What does it mean to be regular?
This amounts to understanding how to apply the fundamental
opera5ons to UDTs
┌────────────────────────┬──────────┐
│ Default construction │ T a; │
├────────────────────────┼──────────┤
│ Copy construction │ T a = b; │
├────────────────────────┼──────────┤
│ Destruction │ ~T(a); │
├────────────────────────┼──────────┤
│ Assignment │ a = b; │
├────────────────────────┼──────────┤
│ Equality │ a == b; │
├────────────────────────┼──────────┤
│ Inequality │ a != b; │
├────────────────────────┼──────────┤
│ Ordering │ a < b; │
└────────────────────────┴──────────┘
What does it mean to be regular?
First, we no,ce that the fundamental opera,ons can be divided
into three groups
┌────────────────────────┬──────────┐
│ │ T a; │
│ ├──────────┤
│ │ T a = b; │
│ Copying and assignment ├──────────┤
│ │ ~T(a); │
│ ├──────────┤
│ │ a = b; │
├────────────────────────┼──────────┤
│ │ a == b; │
│ Equality ├──────────┤
│ │ a != b; │
├────────────────────────┼──────────┤
│ Ordering │ a < b; │
└────────────────────────┴──────────┘
What does it mean to be regular?
Let us now inves,gate in greater detail the proper%es of each such
group
┌────────────────────────┬──────────┐
│ │ T a; │
│ ├──────────┤
│ │ T a = b; │
│ Copying and assignment ├──────────┤
│ │ ~T(a); │
│ ├──────────┤
│ │ a = b; │
├────────────────────────┼──────────┤
│ │ a == b; │
│ Equality ├──────────┤
│ │ a != b; │
├────────────────────────┼──────────┤
│ Ordering │ a < b; │
└────────────────────────┴──────────┘
Copying and assignment
Copy and assignment are interchangeable
We know that built-in types allow wri4ng interchangeably:
T a; a = b; // assignment
T a = b; // copy construction
Copy and assignment are interchangeable
The ra'onale behind this is that it allows natural code such as:
T a;
if (something) a = b;
else a = c;
Default construc.on
Observe that we need default construc.on to make assignment
and copy construc7on interchangeable
T a = b; T a; a = b; T a;
Default construc.on
Note that we require a default constructed regular type to be
assignable and destruc.ble
Par$al construc$on
That is, we require default ini2aliza2on only to par$ally form an
object
Preserving equality
The next step is to note that for all built-in types copy construc5on
and assignment are equality-preserving opera5ons
T a = b; // copy construction
a == b; // true!
Preserving equality
The next step is to note that for all built-in types copy construc5on
and assignment are equality-preserving opera5ons
T a; a = b; // assignment
a == b; // true!
Preserving equality
• Copy construc-ng means to make a copy that is equal to the
original
• Assignment copies the right-hand side object to the le;-hand
side object, leaving their values equal
Dependency on equality
Since we need the ability to test for equality, we have that copy
construc7on and assignment depend on equality
A refined defini)on of regularity
In other words, regular types possess the equality operator and
equality-preserving copy construc4on and assignment
Defining equality
Although equality is conceptually central, we do not yet have a
sa.sfactory defini.on of equality of two objects of a regular type
Equality
Equality for built-in types
On built-in types, the equality rela3on is normally defined as
bitwise equality
Equality for UDTs
Star%ng from this, we can devise a natural defini,on of equality for
user-defined types
Equality for UDTs
Namely, we can define equality of user-defined types from the
equality of its parts
Equality for UDTs
However, in order to build the equality operator from the equality
operator of its parts, we must iden%fy its parts
Example: the point type
struct point {
point(double x, double y): x(x), y(y) {}
double x;
double y;
};
Example: operator== for points
bool operator==(point const& p1, point const& p2) {
return p1.x == p2.x &&
p1.y == p2.y;
}
Remote parts
Complex objects are o&en constructed out of mul0ple simpler
objects, connected by pointers
Remote parts
In such cases, we say that the given complex object has remote
parts
Example: the person type
struct mail_address {...};
class person {
public:
person(...) {...}
private:
std::string name;
std::string surname;
mail_address* address;
};
1st
a&empt: operator== for persons
friend
bool operator==(person const& p1, person const& p2) {
return p1.name == p2.name &&
p1.surname == p2.surname;
}
2nd
a&empt: operator== for persons
friend
bool operator==(person const& p1, person const& p2) {
return p1.name == p2.name &&
p1.surname == p2.surname &&
p1.address == p2.address;
}
3rd
a&empt: operator== for persons
friend
bool operator==(person const& p1, person const& p2) {
return p1.name == p2.name &&
p1.surname == p2.surname &&
*p1.address == *p2.address;
}
Comparing remote parts
For objects with remote parts, the equality operator must compare
the corresponding remote parts, rather than the pointers to them
Comparing remote parts
We do not want objects with equal remote parts to compare
unequal just because the remote parts were in different memory
loca3ons
Defini&on of equality
We can therefore devise the following defini4on of equality:
Two objects are equal if their corresponding parts are equal (applied
recursively), including remote parts (but not comparing their addresses),
excluding inessen>al components, and excluding components which
iden>fy related objects
Equality comes with inequality
It is not enough to define equality, we also need to define
inequality to match it
Unequal vs. not equal
We need to do so in order to preserve the ability to write
interchangeably:
a != b; // unequal
!(a == b); // not equal
Unequal vs. not equal
That is, the statements "two things are unequal to each other" and
"two 6ngs are not equal to each other" should be equivalent
a != b; // unequal
!(a == b); // not equal
Inequality operator
Fortunately, this is very simple:
// T is some user-defined type
bool operator!=(T const& x, T const& y) {
return !(x == y);
}
Equality and inequality
C++ does not enforce that equality and inequality should be
defined together, so remember to define inequality every 7me
equality is defined
Ordering
The importance of ordering
We need to have ordering if we want to find things quickly, as
ordering enables sor$ng, and therefore binary search
Ordering
For defining ordering on a user-defined type we need to set in
place the four rela%onal operators <, >, <=, >=
Rela%onal operators
However, we need to implement only one rela(onal operator,
which can then be used to derive the defini8on on the remaining
three
Less-than operator
We choose < as the primary operator, since when we refer to
ordering we usually mean ascending ordering
Less-than operator seman.cs
Intui&vely, since we are overloading the less-than operator <, the
ordering criterion we implement must behave like <
How to make our overload behave the way that < behaves?
Strictness
First, let us no-ce this natural property of < when used as an
arithme-c operator:
5 ≮ 5 12 ≮ 12 etc.
Strictness
Hence, also our ordering rela0on of choice must be strict, that is:
a ≮ a for all a in T
Is this defini+on strict?
// T is some user-defined type
bool operator<(T const& x, T const& y) {
return true;
}
Is this defini+on strict?
// T is some user-defined type
bool operator<(T const& x, T const& y) {
return true; // according to this 5 < 5,
// which violates strictness
}
Transi'vity
Second, it is immediate to see that the less-than operator is
transi've:
4 < 7 and 7 < 12 implies 4 < 12
Transi'vity
Hence, also our ordering rela0on of choice must be transi've
// T is some user-defined type
bool operator<(T const& x, T const& y) {
// maintain transitivity
}
Trichotomy law
Finally, the less-than operator < obeys the trichotomy law:
For every pair of elements, exactly one of the following holds:
a < b, b < a or a == b
Trichotomy law
Note that the trichotomy law requires the defini5on of < to be
consistent with the defini5on of == so that:
!(a < b) && !(b < a) is equivalent to a == b
Equality is central
Hence, as with assignment and copy, also ordering depends on
equality
!(a < b) && !(b < a) is equivalent to a == b
Is this a well-formed overload?
bool operator<(point const& p1, point const& p2) {
return p1.x < p2.x;
}
Is this a well-formed overload?
It respects strictness
bool operator<(point const& p1, point const& p2) {
return p1.x < p2.x; // (5, 2) ≮ (5, 2)
}
Is this a well-formed overload?
It respects transi,vity
bool operator<(point const& p1, point const& p2) {
return p1.x < p2.x; // (5,2) < (6, 3) < (10, 5)
}
Is this a well-formed overload?
But it does not obey to the trichotomy law
bool operator<(point const& p1, point const& p2) {
return p1.x < p2.x; // (5, 3) ≮ (5, 8) and
// (5, 8) ≮ (5, 3) but...
// (5, 8) ≠ (5, 3)
}
Is this a well-formed overload?
bool operator<(point const& p1, point const& p2) {
if (p1.x < p2.x) return true;
if (p1.x > p2.x) return false;
return p1.y < p2.y;
}
Is this a well-formed overload?
We are imposing a lexicographic ordering over points
bool operator<(point const& p1, point const& p2) {
if (p1.x < p2.x) return true;
if (p1.x > p2.x) return false;
return p1.y < p2.y;
}
Is this a well-formed overload?
Lexicographic ordering respects strictness, transi2vity and the
trichotomy law
bool operator<(point const& p1, point const& p2) {
if (p1.x < p2.x) return true;
if (p1.x > p2.x) return false;
return p1.y < p2.y;
}
Strict total ordering
We refer to an ordering rela.on that is strict, transi.ve and that
obeys the trichotomy law as a strict total ordering
Remaining operators
As with equality, we must define manually the remaining rela5onal
operators >, >=, <=
Greater-than operators
// T is some user-defined type
bool operator>(T const& x, T const& y) {
return y < x;
}
Greater-or-equal-than operator
// T is some user-defined type
bool operator>=(T const& x, T const& y) {
return !(x < y);
}
Less-or-equal-than operator
// T is some user-defined type
bool operator<=(T const& x, T const& y) {
return !(y < x);
}
Semi-regular &
totally ordered types
Regularity
Some%mes, although a type looks like a regular type, we cannot
formally guarantee its regularity
complex seems regular
For instance, the complex type we previously wrote feels like a
regular type
complex a; // default construction
complex b = a; // copy construction
complex c; c = b; // assignment
a == b; // equality
a != c; // inequality
complex numbers cannot be ordered
However, we cannot impose a natural order on complex numbers4
complex a(5, 2);
complex b(8, 0);
a < b; // not defined
4
See, e.g., h)p://math.stackexchange.com/a/488015/79684
complex is non-regular
As such, complex is formally a non-regular type
complex a(5, 2);
complex b(8, 0);
a < b; // not defined
complex is non-regular
However, this might be too restric1ve. For this reason, we opt for a
more fine-grained classifica.on
A relaxed no+on of regularity
We relax our defini.on of regular types and consider regular also
types for which it is not possible to devise a natural order
complex a(5, 2);
complex b(8, 0);
a < b; // not defined
Totally ordered types
We instead refer to regular types for which a natural order is
possible as totally ordered types
Semi-regular types
Similarly, we refer to type that supports equality-preserving copy
and assignment without being equality comparable as semi-regular
Semi-regular types
Intui&vely, this means that a semi-regular type is copyable
Type classifica,on
┌────────────────────────┬──────────┐
│ │ T a; │
│ ├──────────┤
│ │ T a = b; │
│ Semi-regular ├──────────┤
│ │ ~T(a); │
│ ├──────────┤
│ │ a = b; │
├────────────────────────┼──────────┤
│ │ a == b; │
│ Regular ├──────────┤
│ │ a != b; │
├────────────────────────┼──────────┤
│ Totally ordered │ a < b; │
└────────────────────────┴──────────┘
Making vector_int regular
vector_int
class vector_int {
public:
vector_int(): sz(0), elem(nullptr) {}
vector_int(std::size_t sz): sz(sz), elem(new int[sz]) {...}
vector_int(vector_int const& v): sz(v.sz), elem(new int[v.sz]) {...}
~vector_int() {...}
std::size_t size() const {...}
int operator[](std::size_t i) const {...}
int& operator[](std::size_t i) {...}
vector_int& operator=(vector_int const& v) {...}
private:
std::size_t sz;
int* elem;
};
Making vector_int regular
In order to make vector_int regular we need to define
(in)equality and the four rela-on operators
Equality
class vector_int {
public:
...
bool operator==(vector_int const& v) const {
if (sz != v.sz) return false;
for (std::size_t i = 0; i < sz; ++i)
if (elem[i] != v.elem[i]) return false;
return true;
}
};
Less-than operator
class vector_int {
public:
...
bool operator<(vector_int const& v) const {
std::size_t min_size = std::min(sz, v.sz);
std::size_t i = 0;
while (i < min_size && elem[i] == v.elem[i]) ++i;
if (i < min_size)
return elem[i] < v.elem[i];
else
return sz < v.sz;
}
};
Bibliography
Bibliography
• J.C. Dehnert, A. Stepanov, Fundamentals of generic programming
• A. Stepanov, Notes on programming
• A. Stepanov, P. McJones, Elements of programming
Bibliography
• A. Stepanov, D. Rose, From Mathema6cs to Generic
Programming
• A. Stepanov, Efficient Programming with Components (Lecture 1
and Lecture 2)

More Related Content

What's hot

Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++Mihai Todor
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 FeaturesJan Rüegg
 
C traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersC traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersRichard Thomson
 
Csharp4 operators and_casts
Csharp4 operators and_castsCsharp4 operators and_casts
Csharp4 operators and_castsAbed Bukhari
 
C++ memory leak detection
C++ memory leak detectionC++ memory leak detection
C++ memory leak detectionVõ Hòa
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Gamindu Udayanga
 
C++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsC++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsStephane Gleizes
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Geeks Anonymes
 
Generic Programming seminar
Generic Programming seminarGeneric Programming seminar
Generic Programming seminarGautam Roy
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMSfawzmasood
 

What's hot (20)

Modern C++
Modern C++Modern C++
Modern C++
 
Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
 
C traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersC traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmers
 
C++11
C++11C++11
C++11
 
C++11
C++11C++11
C++11
 
Csharp4 operators and_casts
Csharp4 operators and_castsCsharp4 operators and_casts
Csharp4 operators and_casts
 
C++ memory leak detection
C++ memory leak detectionC++ memory leak detection
C++ memory leak detection
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++
 
C++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabsC++17 introduction - Meetup @EtixLabs
C++17 introduction - Meetup @EtixLabs
 
Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)Modern c++ (C++ 11/14)
Modern c++ (C++ 11/14)
 
Generic Programming seminar
Generic Programming seminarGeneric Programming seminar
Generic Programming seminar
 
Summary of C++17 features
Summary of C++17 featuresSummary of C++17 features
Summary of C++17 features
 
Smart pointers
Smart pointersSmart pointers
Smart pointers
 
C++ Presentation
C++ PresentationC++ Presentation
C++ Presentation
 
The Style of C++ 11
The Style of C++ 11The Style of C++ 11
The Style of C++ 11
 
C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
 
C++ references
C++ referencesC++ references
C++ references
 
C Theory
C TheoryC Theory
C Theory
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
 

Viewers also liked

Modern C++ Lunch and Learn
Modern C++ Lunch and LearnModern C++ Lunch and Learn
Modern C++ Lunch and LearnPaul Irwin
 
Metaprogramming in C++ - from 70's to C++17
Metaprogramming in C++ - from 70's to C++17Metaprogramming in C++ - from 70's to C++17
Metaprogramming in C++ - from 70's to C++17Sławomir Zborowski
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++Ilio Catallo
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++Ilio Catallo
 
C++17 - the upcoming revolution (Code::Dive 2015)/
C++17 - the upcoming revolution (Code::Dive 2015)/C++17 - the upcoming revolution (Code::Dive 2015)/
C++17 - the upcoming revolution (Code::Dive 2015)/Sławomir Zborowski
 
C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template LibraryIlio Catallo
 
How to upload to slideshare and embed in blogger
How to upload to slideshare and embed in bloggerHow to upload to slideshare and embed in blogger
How to upload to slideshare and embed in bloggerDaybird1987
 
SYNOPSIS ON BANK MANAGEMENT SYSTEM
SYNOPSIS ON BANK MANAGEMENT SYSTEMSYNOPSIS ON BANK MANAGEMENT SYSTEM
SYNOPSIS ON BANK MANAGEMENT SYSTEMNitish Xavier Tirkey
 
C++のライブラリを簡単に眺めてみよう
C++のライブラリを簡単に眺めてみようC++のライブラリを簡単に眺めてみよう
C++のライブラリを簡単に眺めてみようHiro H.
 

Viewers also liked (12)

Presentation
PresentationPresentation
Presentation
 
Modern C++ Lunch and Learn
Modern C++ Lunch and LearnModern C++ Lunch and Learn
Modern C++ Lunch and Learn
 
Metaprogramming in C++ - from 70's to C++17
Metaprogramming in C++ - from 70's to C++17Metaprogramming in C++ - from 70's to C++17
Metaprogramming in C++ - from 70's to C++17
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Resource wrappers in C++
Resource wrappers in C++Resource wrappers in C++
Resource wrappers in C++
 
Operator overloading in C++
Operator overloading in C++Operator overloading in C++
Operator overloading in C++
 
C++17 - the upcoming revolution (Code::Dive 2015)/
C++17 - the upcoming revolution (Code::Dive 2015)/C++17 - the upcoming revolution (Code::Dive 2015)/
C++17 - the upcoming revolution (Code::Dive 2015)/
 
C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
 
How to upload to slideshare and embed in blogger
How to upload to slideshare and embed in bloggerHow to upload to slideshare and embed in blogger
How to upload to slideshare and embed in blogger
 
SYNOPSIS ON BANK MANAGEMENT SYSTEM
SYNOPSIS ON BANK MANAGEMENT SYSTEMSYNOPSIS ON BANK MANAGEMENT SYSTEM
SYNOPSIS ON BANK MANAGEMENT SYSTEM
 
Bank Management System
Bank Management SystemBank Management System
Bank Management System
 
C++のライブラリを簡単に眺めてみよう
C++のライブラリを簡単に眺めてみようC++のライブラリを簡単に眺めてみよう
C++のライブラリを簡単に眺めてみよう
 

Similar to Regular types in C++

COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants Hemantha Kulathilake
 
Introduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh SinghIntroduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh Singhsinghadarsh
 
cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8kapil078
 
Writing Efficient Code Feb 08
Writing Efficient Code Feb 08Writing Efficient Code Feb 08
Writing Efficient Code Feb 08Ganesh Samarthyam
 
C prog ppt
C prog pptC prog ppt
C prog pptxinoe
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xiiSyed Zaid Irshad
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++Neeru Mittal
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingAlpana Gupta
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingRasan Samarasinghe
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5thConnex
 
C language by Dr. D. R. Gholkar
C language by Dr. D. R. GholkarC language by Dr. D. R. Gholkar
C language by Dr. D. R. GholkarPRAVIN GHOLKAR
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance levelsajjad ali khan
 

Similar to Regular types in C++ (20)

COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants
 
Introduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh SinghIntroduction of C# BY Adarsh Singh
Introduction of C# BY Adarsh Singh
 
C programming
C programmingC programming
C programming
 
cmp104 lec 8
cmp104 lec 8cmp104 lec 8
cmp104 lec 8
 
C programming
C programmingC programming
C programming
 
C# AND F#
C# AND F#C# AND F#
C# AND F#
 
c# at f#
c# at f#c# at f#
c# at f#
 
Writing Efficient Code Feb 08
Writing Efficient Code Feb 08Writing Efficient Code Feb 08
Writing Efficient Code Feb 08
 
Visual c sharp
Visual c sharpVisual c sharp
Visual c sharp
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
What is c
What is cWhat is c
What is c
 
C prog ppt
C prog pptC prog ppt
C prog ppt
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xii
 
Getting started in c++
Getting started in c++Getting started in c++
Getting started in c++
 
C tour Unix
C tour UnixC tour Unix
C tour Unix
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5th
 
C language by Dr. D. R. Gholkar
C language by Dr. D. R. GholkarC language by Dr. D. R. Gholkar
C language by Dr. D. R. Gholkar
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance level
 

More from Ilio Catallo

Multidimensional arrays in C++
Multidimensional arrays in C++Multidimensional arrays in C++
Multidimensional arrays in C++Ilio Catallo
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++Ilio Catallo
 
Spring MVC - Wiring the different layers
Spring MVC -  Wiring the different layersSpring MVC -  Wiring the different layers
Spring MVC - Wiring the different layersIlio Catallo
 
Java and Java platforms
Java and Java platformsJava and Java platforms
Java and Java platformsIlio Catallo
 
Spring MVC - Web Forms
Spring MVC  - Web FormsSpring MVC  - Web Forms
Spring MVC - Web FormsIlio Catallo
 
Spring MVC - The Basics
Spring MVC -  The BasicsSpring MVC -  The Basics
Spring MVC - The BasicsIlio Catallo
 
Introduction To Spring
Introduction To SpringIntroduction To Spring
Introduction To SpringIlio Catallo
 
Gestione della memoria in C++
Gestione della memoria in C++Gestione della memoria in C++
Gestione della memoria in C++Ilio Catallo
 
Puntatori e Riferimenti
Puntatori e RiferimentiPuntatori e Riferimenti
Puntatori e RiferimentiIlio Catallo
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence APIIlio Catallo
 
JSP Standard Tag Library
JSP Standard Tag LibraryJSP Standard Tag Library
JSP Standard Tag LibraryIlio Catallo
 
Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3Ilio Catallo
 
Validation in Jakarta Struts 1.3
Validation in Jakarta Struts 1.3Validation in Jakarta Struts 1.3
Validation in Jakarta Struts 1.3Ilio Catallo
 
Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3Ilio Catallo
 
Community Detection
Community DetectionCommunity Detection
Community DetectionIlio Catallo
 
WWW12 - The CUbRIK Project
WWW12 - The CUbRIK ProjectWWW12 - The CUbRIK Project
WWW12 - The CUbRIK ProjectIlio Catallo
 

More from Ilio Catallo (17)

Multidimensional arrays in C++
Multidimensional arrays in C++Multidimensional arrays in C++
Multidimensional arrays in C++
 
Pointers & References in C++
Pointers & References in C++Pointers & References in C++
Pointers & References in C++
 
Spring MVC - Wiring the different layers
Spring MVC -  Wiring the different layersSpring MVC -  Wiring the different layers
Spring MVC - Wiring the different layers
 
Java and Java platforms
Java and Java platformsJava and Java platforms
Java and Java platforms
 
Spring MVC - Web Forms
Spring MVC  - Web FormsSpring MVC  - Web Forms
Spring MVC - Web Forms
 
Spring MVC - The Basics
Spring MVC -  The BasicsSpring MVC -  The Basics
Spring MVC - The Basics
 
Introduction To Spring
Introduction To SpringIntroduction To Spring
Introduction To Spring
 
Gestione della memoria in C++
Gestione della memoria in C++Gestione della memoria in C++
Gestione della memoria in C++
 
Array in C++
Array in C++Array in C++
Array in C++
 
Puntatori e Riferimenti
Puntatori e RiferimentiPuntatori e Riferimenti
Puntatori e Riferimenti
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
 
JSP Standard Tag Library
JSP Standard Tag LibraryJSP Standard Tag Library
JSP Standard Tag Library
 
Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3Internationalization in Jakarta Struts 1.3
Internationalization in Jakarta Struts 1.3
 
Validation in Jakarta Struts 1.3
Validation in Jakarta Struts 1.3Validation in Jakarta Struts 1.3
Validation in Jakarta Struts 1.3
 
Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3
 
Community Detection
Community DetectionCommunity Detection
Community Detection
 
WWW12 - The CUbRIK Project
WWW12 - The CUbRIK ProjectWWW12 - The CUbRIK Project
WWW12 - The CUbRIK Project
 

Recently uploaded

Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 

Recently uploaded (20)

Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 

Regular types in C++

  • 1. Regular types Ilio Catallo - info@iliocatallo.it
  • 2. Outline • Fundamental opera/ons • Regular types • Semi-regular & totally ordered types • Making vector_int regular • Bibliography
  • 4. Commonali(es among types What is in common between chars and doubles1 ? char c1 = 'a', c2 = 'b'; double d1 = 1.3, d2 = 2.7; 1 For the remainder of this presenta1on, we will limit ourselves to non-singular double values, i.e., we will exclude NaN from the set of possible values
  • 5. Commonali(es among types For a start, both chars and doubles and can be swapped std::swap(c1, c2); std::swap(d1, d2);
  • 7. swap implementa*on for doubles Let us look at the implementa0on of swap as if it was wri4en just for doubles void swap(double& x, double& y) { double tmp = x; x = y; y = tmp; }
  • 8. Requirements on doubles Which requirements on doubles are needed for swap to work? void swap(double& x, double& y) { double tmp = x; x = y; y = tmp; }
  • 9. Requirements on doubles First, it should be possible to copy construct a double void swap(double& x, double& y) { double tmp = x; // tmp is copy constructed from x x = y; y = tmp; }
  • 10. Requirements on doubles Second, it should be possible to assign a double void swap(double& x, double& y) { double tmp = x; x = y; // x is assigned y y = tmp; // y is assigned tmp }
  • 11. Requirements on doubles Given that double values are in fact both assignable and copy construc1ble, we can run swap on them std::swap(d1, d2);
  • 12. swap implementa*on for chars Let us now turn our a,en-on to a version of swap tailored for chars void swap(char& x, char& y) { char tmp = x; x = y; y = tmp; }
  • 13. Requirements on chars As before, we no,ce that char values need to be assignable and copy construc1ble void swap(char& x, char& y) { char tmp = x; x = y; y = tmp; }
  • 14. Requirements on chars As before, since char values are in fact assignable and copy construc1ble, it is possible to swap them std::swap(c1, c2);
  • 15. Swapping built-in types It is immediate to see that our finding generalizes to all built-in types std::swap(b1, b2); // b1 and b2 are bools std::swap(f1, f2); // f1 and f2 are floats std::swap(i1, i2); // i1 and i2 are ints
  • 16. Common opera*ons We conclude that assignment and copy construc0on are opera/ons common to all built-in types
  • 17. Common opera*ons That is, for any built-in type T the following is always possible: T a; a = b; // assignment T c = d; // copy construction
  • 18. Common opera*ons In light of this, we can write a uniform implementa.on of swap for all built-in types void swap(T& x, T& y) { // T is a built-in type T tmp = x; x = y; y = tmp; }
  • 19. Do built-in types share any addi3onal opera3ons?
  • 20. Fundamental opera.ons As a ma&er of fact, we observe that there is a core set of opera4ons which are defined for all built-in types
  • 21. Fundamental opera.ons We call such opera-ons the fundamental opera.ons ┌────────────────────────┬──────────┐ │ Default construction │ T a; │ ├────────────────────────┼──────────┤ │ Copy construction │ T a = b; │ ├────────────────────────┼──────────┤ │ Destruction │ ~T(a); │ ├────────────────────────┼──────────┤ │ Assignment │ a = b; │ ├────────────────────────┼──────────┤ │ Equality │ a == b; │ ├────────────────────────┼──────────┤ │ Inequality │ a != b; │ ├────────────────────────┼──────────┤ │ Ordering │ a < b; │ └────────────────────────┴──────────┘
  • 23. Operators for UDTs The C++ programming language allows the use of built-in type operator syntax for user-defined types (UDTs)
  • 24. Example: the point2 UDT struct point { point(double x, double y): x(x), y(y) {} double x; double y; }; 2 Remember that there is no difference between a struct and a class, up to default members' visibility. Conven=onally, we use structs when our UDTs do not need a private sec=on
  • 25. Example: the point UDT Intui&vely, it makes sense to define operator == as equality: bool operator==(point const& p1, point const& p2) { return p1.x == p2.x && p1.y == p2.y; }
  • 26. Overload seman-cs However, C++ does not dictate any seman&c rules on operator overloading
  • 27. Overload seman-cs That is, developers are free to define == as: bool operator==(point const& p1, point const& p2) { return p1.x * p2.x > p1.y * p2.y; }
  • 28. Seman&cs mismatch This could lead to a mismatch between the expected and the actual seman4cs of an operator
  • 29. Seman&cs mismatch This could lead to a mismatch between the expected and the actual seman4cs of an operator ( °□°
  • 30. Seman&cs mismatch "I admire the commi.ee dedica/on to programming freedom, but I claim that such freedom is an illusion. Finding the laws that govern so=ware components gives us freedom to write complex programs the same way that finding the laws of physics allows us to construct complex mechanical and electrical systems." (A. Stepanov)
  • 31. Built-in types as a theore1cal founda1on The opera)ons common to all built-in types are among the most well-understood and pervasively used
  • 32. Built-in types as a theore1cal founda1on This is because, over the years, the development of built-in types has led to consistent defini9ons that match • the programmer intui-on, and • the underlying mathema-cal understanding
  • 33. Idea: deriving the seman.cs of user-defined types from that of built-in types
  • 34. Preserving opera-on seman-cs The idea is that when a code fragment has a certain meaning for built-in types, it should preserve the same meaning for UDTs
  • 35. Regular types To this end, we introduce the concept of a regular type as a type behaving like the built-in types
  • 36. Regular types We call such types regular, since their use guarantees regularity of behavior and – therefore – interoperability
  • 37. std::vector is regular For instance, std::vector<T> is regular whenever T is regular
  • 38. std::vector is regular As with built-in types, std::vector<T> is thus copy construc+ble and assignable for each regular type T std::vector<int> w = v; // copy construction std::vector<int> u; u = w; // assignment
  • 39. std::vector is swappable Hence, we can swap3 two vectors of any regular type T: std::vector<int> v = {7, 3, 5}; std::vector<int> w = {10, 11, 4}; std::swap(v, w); 3 In reality, for the sake of performance, the Standard Library provides a specialized version std::swap for the specific case of std::vector. Although less performing, the general purpose version would however equally work
  • 40. How can we assure our UDTs to be regular?
  • 41. What does it mean to be regular? Ul#mately, we need to inves#gate how several of the built-in operators should behave when applied to user-defined types
  • 42. What does it mean to be regular? This amounts to understanding how to apply the fundamental opera5ons to UDTs ┌────────────────────────┬──────────┐ │ Default construction │ T a; │ ├────────────────────────┼──────────┤ │ Copy construction │ T a = b; │ ├────────────────────────┼──────────┤ │ Destruction │ ~T(a); │ ├────────────────────────┼──────────┤ │ Assignment │ a = b; │ ├────────────────────────┼──────────┤ │ Equality │ a == b; │ ├────────────────────────┼──────────┤ │ Inequality │ a != b; │ ├────────────────────────┼──────────┤ │ Ordering │ a < b; │ └────────────────────────┴──────────┘
  • 43. What does it mean to be regular? First, we no,ce that the fundamental opera,ons can be divided into three groups ┌────────────────────────┬──────────┐ │ │ T a; │ │ ├──────────┤ │ │ T a = b; │ │ Copying and assignment ├──────────┤ │ │ ~T(a); │ │ ├──────────┤ │ │ a = b; │ ├────────────────────────┼──────────┤ │ │ a == b; │ │ Equality ├──────────┤ │ │ a != b; │ ├────────────────────────┼──────────┤ │ Ordering │ a < b; │ └────────────────────────┴──────────┘
  • 44. What does it mean to be regular? Let us now inves,gate in greater detail the proper%es of each such group ┌────────────────────────┬──────────┐ │ │ T a; │ │ ├──────────┤ │ │ T a = b; │ │ Copying and assignment ├──────────┤ │ │ ~T(a); │ │ ├──────────┤ │ │ a = b; │ ├────────────────────────┼──────────┤ │ │ a == b; │ │ Equality ├──────────┤ │ │ a != b; │ ├────────────────────────┼──────────┤ │ Ordering │ a < b; │ └────────────────────────┴──────────┘
  • 46. Copy and assignment are interchangeable We know that built-in types allow wri4ng interchangeably: T a; a = b; // assignment T a = b; // copy construction
  • 47. Copy and assignment are interchangeable The ra'onale behind this is that it allows natural code such as: T a; if (something) a = b; else a = c;
  • 48. Default construc.on Observe that we need default construc.on to make assignment and copy construc7on interchangeable T a = b; T a; a = b; T a;
  • 49. Default construc.on Note that we require a default constructed regular type to be assignable and destruc.ble
  • 50. Par$al construc$on That is, we require default ini2aliza2on only to par$ally form an object
  • 51. Preserving equality The next step is to note that for all built-in types copy construc5on and assignment are equality-preserving opera5ons T a = b; // copy construction a == b; // true!
  • 52. Preserving equality The next step is to note that for all built-in types copy construc5on and assignment are equality-preserving opera5ons T a; a = b; // assignment a == b; // true!
  • 53. Preserving equality • Copy construc-ng means to make a copy that is equal to the original • Assignment copies the right-hand side object to the le;-hand side object, leaving their values equal
  • 54. Dependency on equality Since we need the ability to test for equality, we have that copy construc7on and assignment depend on equality
  • 55. A refined defini)on of regularity In other words, regular types possess the equality operator and equality-preserving copy construc4on and assignment
  • 56. Defining equality Although equality is conceptually central, we do not yet have a sa.sfactory defini.on of equality of two objects of a regular type
  • 58. Equality for built-in types On built-in types, the equality rela3on is normally defined as bitwise equality
  • 59. Equality for UDTs Star%ng from this, we can devise a natural defini,on of equality for user-defined types
  • 60. Equality for UDTs Namely, we can define equality of user-defined types from the equality of its parts
  • 61. Equality for UDTs However, in order to build the equality operator from the equality operator of its parts, we must iden%fy its parts
  • 62. Example: the point type struct point { point(double x, double y): x(x), y(y) {} double x; double y; };
  • 63. Example: operator== for points bool operator==(point const& p1, point const& p2) { return p1.x == p2.x && p1.y == p2.y; }
  • 64. Remote parts Complex objects are o&en constructed out of mul0ple simpler objects, connected by pointers
  • 65. Remote parts In such cases, we say that the given complex object has remote parts
  • 66. Example: the person type struct mail_address {...}; class person { public: person(...) {...} private: std::string name; std::string surname; mail_address* address; };
  • 67. 1st a&empt: operator== for persons friend bool operator==(person const& p1, person const& p2) { return p1.name == p2.name && p1.surname == p2.surname; }
  • 68. 2nd a&empt: operator== for persons friend bool operator==(person const& p1, person const& p2) { return p1.name == p2.name && p1.surname == p2.surname && p1.address == p2.address; }
  • 69. 3rd a&empt: operator== for persons friend bool operator==(person const& p1, person const& p2) { return p1.name == p2.name && p1.surname == p2.surname && *p1.address == *p2.address; }
  • 70. Comparing remote parts For objects with remote parts, the equality operator must compare the corresponding remote parts, rather than the pointers to them
  • 71. Comparing remote parts We do not want objects with equal remote parts to compare unequal just because the remote parts were in different memory loca3ons
  • 72. Defini&on of equality We can therefore devise the following defini4on of equality: Two objects are equal if their corresponding parts are equal (applied recursively), including remote parts (but not comparing their addresses), excluding inessen>al components, and excluding components which iden>fy related objects
  • 73. Equality comes with inequality It is not enough to define equality, we also need to define inequality to match it
  • 74. Unequal vs. not equal We need to do so in order to preserve the ability to write interchangeably: a != b; // unequal !(a == b); // not equal
  • 75. Unequal vs. not equal That is, the statements "two things are unequal to each other" and "two 6ngs are not equal to each other" should be equivalent a != b; // unequal !(a == b); // not equal
  • 76. Inequality operator Fortunately, this is very simple: // T is some user-defined type bool operator!=(T const& x, T const& y) { return !(x == y); }
  • 77. Equality and inequality C++ does not enforce that equality and inequality should be defined together, so remember to define inequality every 7me equality is defined
  • 79. The importance of ordering We need to have ordering if we want to find things quickly, as ordering enables sor$ng, and therefore binary search
  • 80. Ordering For defining ordering on a user-defined type we need to set in place the four rela%onal operators <, >, <=, >=
  • 81. Rela%onal operators However, we need to implement only one rela(onal operator, which can then be used to derive the defini8on on the remaining three
  • 82. Less-than operator We choose < as the primary operator, since when we refer to ordering we usually mean ascending ordering
  • 83. Less-than operator seman.cs Intui&vely, since we are overloading the less-than operator <, the ordering criterion we implement must behave like <
  • 84. How to make our overload behave the way that < behaves?
  • 85. Strictness First, let us no-ce this natural property of < when used as an arithme-c operator: 5 ≮ 5 12 ≮ 12 etc.
  • 86. Strictness Hence, also our ordering rela0on of choice must be strict, that is: a ≮ a for all a in T
  • 87. Is this defini+on strict? // T is some user-defined type bool operator<(T const& x, T const& y) { return true; }
  • 88. Is this defini+on strict? // T is some user-defined type bool operator<(T const& x, T const& y) { return true; // according to this 5 < 5, // which violates strictness }
  • 89. Transi'vity Second, it is immediate to see that the less-than operator is transi've: 4 < 7 and 7 < 12 implies 4 < 12
  • 90. Transi'vity Hence, also our ordering rela0on of choice must be transi've // T is some user-defined type bool operator<(T const& x, T const& y) { // maintain transitivity }
  • 91. Trichotomy law Finally, the less-than operator < obeys the trichotomy law: For every pair of elements, exactly one of the following holds: a < b, b < a or a == b
  • 92. Trichotomy law Note that the trichotomy law requires the defini5on of < to be consistent with the defini5on of == so that: !(a < b) && !(b < a) is equivalent to a == b
  • 93. Equality is central Hence, as with assignment and copy, also ordering depends on equality !(a < b) && !(b < a) is equivalent to a == b
  • 94. Is this a well-formed overload? bool operator<(point const& p1, point const& p2) { return p1.x < p2.x; }
  • 95. Is this a well-formed overload? It respects strictness bool operator<(point const& p1, point const& p2) { return p1.x < p2.x; // (5, 2) ≮ (5, 2) }
  • 96. Is this a well-formed overload? It respects transi,vity bool operator<(point const& p1, point const& p2) { return p1.x < p2.x; // (5,2) < (6, 3) < (10, 5) }
  • 97. Is this a well-formed overload? But it does not obey to the trichotomy law bool operator<(point const& p1, point const& p2) { return p1.x < p2.x; // (5, 3) ≮ (5, 8) and // (5, 8) ≮ (5, 3) but... // (5, 8) ≠ (5, 3) }
  • 98. Is this a well-formed overload? bool operator<(point const& p1, point const& p2) { if (p1.x < p2.x) return true; if (p1.x > p2.x) return false; return p1.y < p2.y; }
  • 99. Is this a well-formed overload? We are imposing a lexicographic ordering over points bool operator<(point const& p1, point const& p2) { if (p1.x < p2.x) return true; if (p1.x > p2.x) return false; return p1.y < p2.y; }
  • 100. Is this a well-formed overload? Lexicographic ordering respects strictness, transi2vity and the trichotomy law bool operator<(point const& p1, point const& p2) { if (p1.x < p2.x) return true; if (p1.x > p2.x) return false; return p1.y < p2.y; }
  • 101. Strict total ordering We refer to an ordering rela.on that is strict, transi.ve and that obeys the trichotomy law as a strict total ordering
  • 102. Remaining operators As with equality, we must define manually the remaining rela5onal operators >, >=, <=
  • 103. Greater-than operators // T is some user-defined type bool operator>(T const& x, T const& y) { return y < x; }
  • 104. Greater-or-equal-than operator // T is some user-defined type bool operator>=(T const& x, T const& y) { return !(x < y); }
  • 105. Less-or-equal-than operator // T is some user-defined type bool operator<=(T const& x, T const& y) { return !(y < x); }
  • 107. Regularity Some%mes, although a type looks like a regular type, we cannot formally guarantee its regularity
  • 108. complex seems regular For instance, the complex type we previously wrote feels like a regular type complex a; // default construction complex b = a; // copy construction complex c; c = b; // assignment a == b; // equality a != c; // inequality
  • 109. complex numbers cannot be ordered However, we cannot impose a natural order on complex numbers4 complex a(5, 2); complex b(8, 0); a < b; // not defined 4 See, e.g., h)p://math.stackexchange.com/a/488015/79684
  • 110. complex is non-regular As such, complex is formally a non-regular type complex a(5, 2); complex b(8, 0); a < b; // not defined
  • 111. complex is non-regular However, this might be too restric1ve. For this reason, we opt for a more fine-grained classifica.on
  • 112. A relaxed no+on of regularity We relax our defini.on of regular types and consider regular also types for which it is not possible to devise a natural order complex a(5, 2); complex b(8, 0); a < b; // not defined
  • 113. Totally ordered types We instead refer to regular types for which a natural order is possible as totally ordered types
  • 114. Semi-regular types Similarly, we refer to type that supports equality-preserving copy and assignment without being equality comparable as semi-regular
  • 115. Semi-regular types Intui&vely, this means that a semi-regular type is copyable
  • 116. Type classifica,on ┌────────────────────────┬──────────┐ │ │ T a; │ │ ├──────────┤ │ │ T a = b; │ │ Semi-regular ├──────────┤ │ │ ~T(a); │ │ ├──────────┤ │ │ a = b; │ ├────────────────────────┼──────────┤ │ │ a == b; │ │ Regular ├──────────┤ │ │ a != b; │ ├────────────────────────┼──────────┤ │ Totally ordered │ a < b; │ └────────────────────────┴──────────┘
  • 118. vector_int class vector_int { public: vector_int(): sz(0), elem(nullptr) {} vector_int(std::size_t sz): sz(sz), elem(new int[sz]) {...} vector_int(vector_int const& v): sz(v.sz), elem(new int[v.sz]) {...} ~vector_int() {...} std::size_t size() const {...} int operator[](std::size_t i) const {...} int& operator[](std::size_t i) {...} vector_int& operator=(vector_int const& v) {...} private: std::size_t sz; int* elem; };
  • 119. Making vector_int regular In order to make vector_int regular we need to define (in)equality and the four rela-on operators
  • 120. Equality class vector_int { public: ... bool operator==(vector_int const& v) const { if (sz != v.sz) return false; for (std::size_t i = 0; i < sz; ++i) if (elem[i] != v.elem[i]) return false; return true; } };
  • 121. Less-than operator class vector_int { public: ... bool operator<(vector_int const& v) const { std::size_t min_size = std::min(sz, v.sz); std::size_t i = 0; while (i < min_size && elem[i] == v.elem[i]) ++i; if (i < min_size) return elem[i] < v.elem[i]; else return sz < v.sz; } };
  • 123. Bibliography • J.C. Dehnert, A. Stepanov, Fundamentals of generic programming • A. Stepanov, Notes on programming • A. Stepanov, P. McJones, Elements of programming
  • 124. Bibliography • A. Stepanov, D. Rose, From Mathema6cs to Generic Programming • A. Stepanov, Efficient Programming with Components (Lecture 1 and Lecture 2)