Commit c078f4d2 authored by CHAMONT David's avatar CHAMONT David
Browse files

Preparation de la session On-Demand 3/1

parent c6b5c822
......@@ -9,7 +9,7 @@ The more we progress in the lessons, the more advanced C++ features we will use:
For some of the exercices, you will need to have **GNU time** installed where you want to execute the scripts. Try the command `\time -f "%U" echo toto` to check if it works fine. The `\` is there to avoid that bash use its own internal time command. If it does not work properly, try `conda install -c conda-forge time` (for example).
## If you want to work with thenotebooks
## If you want to work with the notebooks
Most of the education materials is given as notebooks. You can read them on the [public project](https://gitlab.in2p3.fr/chamont/modernscientificcpp), and copy/paste the C++ extracts. Yet, if you want to play the notebooks directly on your machine, you will need to install:
1. `git` and `git-lfs` so to dowload the files (see en.teaching-material.md) ;
......@@ -18,7 +18,7 @@ Most of the education materials is given as notebooks. You can read them on the
The jupyter notebooks tools are part of the [anaconda3 distribution](https://www.anaconda.com/). One can also use `miniconda` for a more focused installation (see below).
## Basic installation and setup with miniconda
### Basic installation and setup with miniconda
[Miniconda](https://docs.conda.io/en/latest/miniconda.html) may be installed this way:
```
......@@ -44,7 +44,7 @@ mamba install -y -c conda-forge gxx
## With docker
We have also prepared some docker images with anything pre-installed, if you have docker on your machine. See en.docker.md .
We have also prepared some docker images with anything pre-installed, if you have docker on your machine. See `docker.md` .
......@@ -2,7 +2,8 @@
# About this course notebooks
The use of notebooks is not completely mandatory : you can read them on the [public project](https://gitlab.in2p3.fr/chamont/modernscientificcpp), and copy/paste the C++ extracts to your prefered editor. Else, if you want to play with the notebooks,
please look at the notebook section in file `en.instalation.md`.
please look at the notebook section in file `installation.md`.
## Two kind of notebooks
......
......@@ -367,6 +367,216 @@
"col.display() ;"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"#### Beware if you mix it with other constructors : `std::initializer_list<T>` has priority."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"attributes": {
"classes": [
"cpp"
],
"id": ""
},
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"#include <iostream>\n",
"\n",
"template <typename T>\n",
"class MyCollection\n",
" {\n",
" public:\n",
" MyCollection( std::initializer_list<T> const & )\n",
" { std::cout<<\"MyCollection( std::initializer_list<T> const & )\"<<std::endl ; }\n",
" MyCollection( int size, T value )\n",
" { std::cout<<\"MyCollection( ( int size, int value ) )\"<<std::endl ; }\n",
" } ;"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"attributes": {
"classes": [
"cpp"
],
"id": ""
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MyCollection( std::initializer_list<T> const & )\n"
]
}
],
"source": [
"MyCollection col { 1, 2, 3 } ;"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"attributes": {
"classes": [
"cpp"
],
"id": ""
},
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MyCollection( std::initializer_list<T> const & )\n"
]
}
],
"source": [
"MyCollection col { 1, 2 } ;"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"attributes": {
"classes": [
"cpp"
],
"id": ""
},
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MyCollection( ( int size, int value ) )\n"
]
}
],
"source": [
"MyCollection col ( 1, 2 ) ;"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"#### This is the case for `std::vector`"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"attributes": {
"classes": [
"cpp"
],
"id": ""
},
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [],
"source": [
"#include <iostream>\n",
"#include <vector>"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"attributes": {
"classes": [
"cpp"
],
"id": ""
},
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 1 "
]
}
],
"source": [
"std::vector<int> col { 3, 1 } ;\n",
"for ( int elem : col )\n",
"{ std::cout<<elem<<\" \" ; }"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"attributes": {
"classes": [
"cpp"
],
"id": ""
},
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 1 1 "
]
}
],
"source": [
"std::vector<int> col ( 3, 1 ) ;\n",
"for ( int elem : col )\n",
"{ std::cout<<elem<<\" \" ; }"
]
},
{
"cell_type": "markdown",
"metadata": {
......@@ -469,7 +679,7 @@
}
},
"source": [
"© *CNRS 2019* \n",
"© *CNRS 2022* \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/)*"
]
}
......
......@@ -238,7 +238,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 11,
"metadata": {
"slideshow": {
"slide_type": "skip"
......@@ -251,38 +251,20 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 19,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_9:1:36: error: no member named 'cout' in namespace 'std'\n",
"void process( int & ) { std::cout<<\"process(&)\"<<std::endl ; }\n",
" ~~~~~^\n"
]
},
{
"ename": "Interpreter Error",
"evalue": "",
"output_type": "error",
"traceback": [
"Interpreter Error: "
]
}
],
"outputs": [],
"source": [
"void process( int & ) { std::cout<<\"process(&)\"<<std::endl ; }"
"void process( int & value ) { std::cout<<\"process(&): \"<<value<<std::endl ; }"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 20,
"metadata": {
"slideshow": {
"slide_type": "-"
......@@ -290,12 +272,12 @@
},
"outputs": [],
"source": [
"void process( int const & ) { std::cout<<\"process(const &)\"<<std::endl ; }"
"void process( int const & value ) { std::cout<<\"process(const &): \"<<value<<std::endl ; }"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 21,
"metadata": {
"slideshow": {
"slide_type": "-"
......@@ -303,12 +285,12 @@
},
"outputs": [],
"source": [
"void process( int && ) { std::cout<<\"process(&&)\"<<std::endl ; }"
"void process( int && value ) { std::cout<<\"process(&&): \"<<value<<std::endl ; }"
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 22,
"metadata": {
"attributes": {
"classes": [
......@@ -328,7 +310,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 23,
"metadata": {
"attributes": {
"classes": [
......@@ -342,20 +324,10 @@
},
"outputs": [
{
"name": "stderr",
"name": "stdout",
"output_type": "stream",
"text": [
"input_line_8:2:2: error: use of undeclared identifier 'process'\n",
" process(i) ;\n",
" ^\n"
]
},
{
"ename": "Interpreter Error",
"evalue": "",
"output_type": "error",
"traceback": [
"Interpreter Error: "
"process(&): 42\n"
]
}
],
......@@ -365,7 +337,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 24,
"metadata": {
"attributes": {
"classes": [
......@@ -382,7 +354,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"process(const &)\n"
"process(const &): 42\n"
]
}
],
......@@ -392,7 +364,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 25,
"metadata": {
"attributes": {
"classes": [
......@@ -409,7 +381,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"process(&&)\n"
"process(&&): 42\n"
]
}
],
......@@ -441,7 +413,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 9,
"metadata": {
"slideshow": {
"slide_type": "skip"
......@@ -455,56 +427,20 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 26,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"input_line_10:4:3: error: no member named 'cout' in namespace 'std'; did you mean 'col'?\n",
" std::cout<<title<<\":\" ;\n",
" ^~~~~~~~~\n",
" col\n",
"input_line_10:2:61: note: 'col' declared here\n",
"void display( std::string const & title, Collection const & col )\n",
" ^\n",
"input_line_10:6:6: error: no member named 'cout' in namespace 'std'; did you mean 'col'?\n",
" { std::cout<<\" \"<<element ; }\n",
" ^~~~~~~~~\n",
" col\n",
"input_line_10:2:61: note: 'col' declared here\n",
"void display( std::string const & title, Collection const & col )\n",
" ^\n",
"input_line_10:7:3: error: no member named 'cout' in namespace 'std'; did you mean 'col'?\n",
" std::cout<<std::endl ;\n",
" ^~~~~~~~~\n",
" col\n",
"input_line_10:2:61: note: 'col' declared here\n",
"void display( std::string const & title, Collection const & col )\n",
" ^\n"
]
},
{
"ename": "Interpreter Error",
"evalue": "",
"output_type": "error",
"traceback": [
"Interpreter Error: "
]
}
],
"outputs": [],
"source": [
"template < typename Collection >\n",
"void display( std::string const & title, Collection const & col )\n",
" {\n",
" std::cout<<title<<\":\" ;\n",
" for ( auto element : col )\n",
" { std::cout<<\" \"<<element ; }\n",
" for ( auto elem : col )\n",
" { std::cout<<\" \"<<elem ; }\n",
" std::cout<<std::endl ;\n",
" }"
]
......
......@@ -35,7 +35,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "skip"
......@@ -48,7 +48,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "skip"
......@@ -56,12 +56,12 @@
},
"outputs": [],
"source": [
"void process(char *) { std::cout<<\"process(char *)\"<<std::endl ; }"
"void process( char * a_value ) { std::cout<<\"process(char *): \"<<a_value<<std::endl ; }"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 3,
"metadata": {
"slideshow": {
"slide_type": "skip"
......@@ -69,12 +69,12 @@
},
"outputs": [],
"source": [
"void process(int) { std::cout<<\"process(int)\"<<std::endl ; }"
"void process( int a_value ) { std::cout<<\"process(int): \"<<a_value<<std::endl ; }"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "skip"
......@@ -82,12 +82,12 @@
},
"outputs": [],
"source": [
"void process(long) { std::cout<<\"process(long)\"<<std::endl ; }"
"void process( long a_value ) { std::cout<<\"process(long): \"<<a_value<<std::endl ; }"
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 5,
"metadata": {
"slideshow": {
"slide_type": "skip"
......@@ -100,7 +100,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 6,
"metadata": {
"slideshow": {
"slide_type": "skip"
......@@ -111,7 +111,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"process(int)\n"
"process(int): 0\n"
]
}
],
......@@ -121,7 +121,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "skip"
......@@ -132,7 +132,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"process(int)\n"
"process(int): 0\n"
]
}
],
......@@ -142,7 +142,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 8,
"metadata": {
"slideshow": {
"slide_type": "skip"
......@@ -153,7 +153,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"process(long)\n"
"process(long): 0\n"
]
}
],
......@@ -185,7 +185,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 9,
"metadata": {
"slideshow": {
"slide_type": "skip"
......@@ -196,7 +196,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"process(char *)\n"
"process(char *): "
]
}
],
......@@ -223,10 +223,10 @@
}
},
"source": [
"## `std::unique_ptr`\n",
"## `shared_ptr`\n",
"\n",
"* The most efficient way to handle objects made with `new`.\n",
"* A kind of guard which is **moving the ownership**.\n",
"* The easiest way to handle objects made with `new`... and the slowest.\n",
"* A kind of **guard** which is **counting the references**.\n",
"* Provides dereferencing operators **\\*** and **->**. "
]
},
......@@ -238,19 +238,13 @@
}
},
"source": [
"#### Difficulty: it is \"move-only\""
"#### A copyable pointer"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 1,
"metadata": {
"attributes": {
"classes": [
"cpp"
],
"id": ""
},
"slideshow": {