Commit 33b3ad07 authored by CHAMONT David's avatar CHAMONT David
Browse files

3/ and 4/ ready.

parent d5c847ae
......@@ -890,7 +890,11 @@
"* https://en.cppreference.com/w/cpp/language/types\n",
"* https://en.cppreference.com/w/cpp/language/implicit_conversion\n",
"* https://www.learncpp.com/cpp-tutorial/floating-point-numbers/\n",
"* https://dbj.org/how-to-avoid-implicit-conversion-in-c/"
"* https://dbj.org/how-to-avoid-implicit-conversion-in-c/\n",
"\n",
"Floating point arithmetic\n",
"* [What Every Programmer Should Know About Floating-Point Arithmetic](https://floating-point-gui.de/)\n",
"* [IEEE-754 Floating-Point Conversion](https://babbage.cs.qc.cuny.edu/IEEE-754.old/Decimal.html)"
]
},
{
......@@ -923,4 +927,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
\ No newline at end of file
......@@ -460,7 +460,13 @@
"* https://en.cppreference.com/w/cpp/language/types\n",
"* https://en.cppreference.com/w/cpp/language/implicit_conversion\n",
"* https://www.learncpp.com/cpp-tutorial/floating-point-numbers/\n",
"* https://dbj.org/how-to-avoid-implicit-conversion-in-c/"
"* https://dbj.org/how-to-avoid-implicit-conversion-in-c/\n",
"\n",
"\n",
"Arithmetique flottante\n",
"* [What Every Programmer Should Know About Floating-Point Arithmetic](https://floating-point-gui.de/)\n",
"* [IEEE-754 Floating-Point Conversion](https://babbage.cs.qc.cuny.edu/IEEE-754.old/Decimal.html)\n",
"* [CPPFrUG 41 - Les nombres à virgule flottante, à l'arrondi près - Vivien Mille](https://www.youtube.com/watch?v=lfipqoQHuPU)"
]
},
{
......
......@@ -646,7 +646,7 @@
"source": [
"## Exercise\n",
"\n",
"Eliminate the raw pointers from the example."
"Eliminate the raw pointers from the example, and use smart pointers instead, so that the explicit call to `delete` in `main()` can be removed."
]
},
{
......
......@@ -396,13 +396,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting tmp.syntactic-sugar.cpp\n"
]
}
],
"source": [
"%%file tmp.syntactic-sugar.cpp\n",
"\n",
......@@ -416,14 +424,15 @@
"\n",
"int main()\n",
" {\n",
" std::cout<<42_K<<std::endl ;\n",
" int var = -42_K ;\n",
" std::cout<<var<<std::endl ;\n",
" std::cout<<3.14_K<<std::endl ;\n",
" }"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 22,
"metadata": {
"slideshow": {
"slide_type": "fragment"
......@@ -436,13 +445,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-42000\n",
"3140\n"
]
}
],
"source": [
"!./tmp.syntactic-sugar.exe"
]
......@@ -493,7 +511,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "fragment"
......@@ -506,7 +524,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "fragment"
......@@ -519,7 +537,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {
"slideshow": {
"slide_type": "fragment"
......@@ -532,7 +550,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "fragment"
......@@ -545,13 +563,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"int\n",
"int\n",
"long\n"
]
}
],
"source": [
"const int NULLPTR = 0 ;\n",
"fnul(0) ;\n",
......@@ -572,13 +600,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"char *\n"
]
}
],
"source": [
"void * const NULLPTR = (void *)0 ;\n",
"fnul((char *)NULLPTR) ; // appel de f(char *)"
......@@ -597,13 +633,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"char *\n"
]
}
],
"source": [
"fnul(nullptr) ;"
]
......
......@@ -623,13 +623,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Writing tmp.type-inference.cpp\n"
]
}
],
"source": [
"%%file tmp.type-inference.cpp\n",
"\n",
......@@ -649,7 +657,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "fragment"
......@@ -662,13 +670,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"42\n",
"3.14\n",
"3.14\n"
]
}
],
"source": [
"!./tmp.type-inference.exe"
]
......
......@@ -877,9 +877,11 @@
},
"outputs": [],
"source": [
"std::vector<int> numbers = { 1, 2, 3, 4, 5 } ;\n",
"auto mult = make_multiplier(3) ;\n",
"std::for_each(numbers.begin(),numbers.end(),mult) ;"
"{\n",
" std::vector<int> numbers = { 1, 2, 3, 4, 5 } ;\n",
" auto mult = make_multiplier(3) ;\n",
" std::for_each(numbers.begin(),numbers.end(),mult) ;\n",
"}"
]
},
{
......
#include <iostream>
#include <array>
template <typename T>
class my_span
{
public :
template <typename Col>
my_span( Col & c ) : p0_(&c[0]), size_(c.size()) {}
T & operator[]( std::size_t indice ) { return *(p0_+indice) ; }
std::size_t size() { return size_ ; }
private :
T * const p0_ ;
const std::size_t size_ ;
} ;
template <typename T>
void print( my_span<T> data )
{
for( std::size_t i = 0 ; i < data.size() ; ++i )
std::cout << data[i] << ' ' ;
std::cout << std::endl ;
}
int main()
{
std::array<int,5> arr = { 1, 2, 3, 4, 5 } ;
my_span<int> s { arr } ;
print(s) ;
return 0 ;
}
#include <iostream>
#include <type_traits>
#include <cassert>
class Demo
{
public:
Demo() { std::cout<<"Constructor"<<std::endl ; }
void print() { std::cout<<"Printing"<<std::endl ; }
~Demo() { std::cout<<"Destructor"<<std::endl ; }
} ;
template <typename T, std::enable_if_t<std::is_pointer_v<T>, int> * = nullptr>
using my_owner = T ;
template <typename T, std::enable_if_t<std::is_pointer_v<T>, int> * = nullptr>
class my_not_null
{
public :
my_not_null( T p ) : p_(p) { assert(p) ; }
my_not_null & operator=( T p ) { assert(p) ; p_ = p ; return *this ; }
T operator->() { return p_ ; }
private :
T p_ ;
} ;
int main()
{
//my_owner<Demo> d1 ; // COMPILATION ERROR: Demo is not a pointer
//my_not_null<Demo*> p1 ; // COMPILATION ERROR: p1 is not initialized
//my_not_null<Demo*> p2(nullptr) ; // COMPILATION ERROR: p2 cannot be null
my_owner<Demo *> d2 = new Demo() ;
my_not_null<Demo *> p3 = d2 ;
//p3 = nullptr ; // EXECUTION ERROR: p3 cannot be null
p3->print() ;
delete d2 ;
}
#include <iostream>
#include <cassert>
template <typename TypeOut>
struct no_narrow
{
TypeOut data_ ;
template <typename TypeIn>
no_narrow( TypeIn data ) : data_(data)
{ assert(static_cast<TypeOut>(data)==data) ; }
operator TypeOut() { return data_ ; }
} ;
int main()
{
double d1 = 42 ;
int i1 = no_narrow<int>(d1) ;
std::cout<<i1<<std::endl ;
double d2 = 3.14 ;
int i2 = no_narrow<int>(d2) ;
std::cout<<i2<<std::endl ;
}
\ No newline at end of file
#include <iostream>
template <typename Function>
struct FinalAction
{
Function f_ ;
FinalAction( Function f ) : f_(f) {}
~FinalAction() { f_() ; }
} ;
template <typename Function>
auto my_finally( Function f )
{ return FinalAction<Function>(f) ; }
int main()
{
std::cout<<"Step 1"<<std::endl ;
auto _ = my_finally([](){ std::cout<<"Step 3"<<std::endl ; }) ;
std::cout<<"Step 2"<<std::endl ;
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Cpp Core Guidelines & Guidelines Support Library"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"2015 : Stroustrup and Sutter formalized a draft [coding guidelines](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines), established on an \"informal\" basis with a few experts. This tends to be now the reference for good practices."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### Goals\n",
"* Facilitate the adoption of modern C ++.\n",
"* Reference and reinforce good practices.\n",
"* Favor automatically verifiable rules, and thus promote the emergence of verification tools."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"The rules specify a header library called **Guideline Support Library (GSL)**\n",
"* [Microsoft implementation](https://github.com/microsoft/gsl) requires C++14.\n",
"* The one implemented by Martin Moene, [GSL-lite](https://github.com/martinmoene/gsl-lite), is operational starting C++98.\n",
"* Some of the GSL proposals have been incorporated into C++11/14/17/20."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"A common point to many proposals is an increase of abstraction. Instead of just exposing the implementation and the language mechanisms used, they strive to **better express the developer's intention**."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"### To be continued\n",
"* [Pointers](en.1-gsl-pointers.ipynb)\n",
"* [Arrays](en.2-gsl-arrays.ipynb)\n",
"* [Utilities](en.3-gsl-utilities.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"© *CNRS 2021* \n",
"*This document was created by David Chamont and translated by Olga Abramkina. It is available under the [License Creative Commons - Attribution - No commercial use - Shared under the conditions 4.0 International](http://creativecommons.org/licenses/by-nc-sa/4.0/)*"
]
}
],
"metadata": {
"celltoolbar": "Diaporama",
"kernelspec": {
"display_name": "C++17",
"language": "C++17",
"name": "xcpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "17"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# GSL pointers\n",
"\n",
"Many of the problems with pointers come from their multiple uses. They point indifferently to:\n",
"* memory areas of the stack or of the heap,\n",
"* single objects or tables,\n",
"* heap zones whose release is managed elsewhere,\n",
"* heap zones to be released at the end of the current instruction block.\n",
"\n",
"**Let's sort it out and express these uses more clearly**."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Raw pointers and references: `T*` and `T&`"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"A \"raw\" pointer (e.g. `T *`) is supposed to have the most ordinary meaning: **it points to an object, but does not \"own\" it**. In what follows, the portion of code that uses this pointer is not supposed to do a `delete` at the end of the use."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"If the pointer itself is not meant to change value, and is never meant to be zero, **we will prefer a reference** whenever possible."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"In addition, using a raw pointer to designate an array is to be avoided. Many other solutions exist now."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"## Non-zero pointers: `gsl::not_null<T>`\n",
"\n",
"When dereferencing a pointer (via `*` or `->`), a common practice is to first check that it is not null as a precaution, which obscures and slows down the code."
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Whenever possible, **replace this pointer with a reference**."
]
},
{