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

After 3-CollectiveDay

parent 33b3ad07
......@@ -5,9 +5,11 @@ 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) ; }
explicit 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_ ;
......
......@@ -95,7 +95,7 @@
}
},
"source": [
"**Otherwise**, use **`gsl :: not_null <T>`**, which ensures that you never assign a null value to the pointer."
"**Otherwise**, use **`gsl::not_null<T>`**, which ensures that you never assign a null value to the pointer."
]
},
{
......@@ -135,7 +135,7 @@
" // ...\n",
" Demo * d2 = nullptr ;\n",
" // ...\n",
" gsl::not_null<Demo *> d3 { d2 } ;\n",
" gsl::not_null<Demo *> d3 { d2 } ; \n",
" // ...\n",
" gsl::not_null<Demo *> d4 { nullptr } ;\n",
"}"
......@@ -186,7 +186,7 @@
}
},
"source": [
"The type `owner <T>` is used to mark that a pointer owns the pointed object. `T` is assumed to be a pointer type, such as `int *`."
"The type `owner<T>` is used to mark that a pointer owns the pointed object. `T` is assumed to be a pointer type, such as `int *`."
]
},
{
......@@ -197,7 +197,7 @@
}
},
"source": [
"The type `owner <T>` does nothing in itself: you are still in charge of making the call to `delete`, but it clarifies your intentions and can allow static checking tools to detect a forgotten delete."
"The type `owner<T>` does nothing in itself: you are still in charge of making the call to `delete`, but it clarifies your intentions and can allow static checking tools to detect a forgotten delete."
]
},
{
......@@ -230,7 +230,7 @@
"\n",
"int main() {\n",
" gsl::owner<Demo *> d = new Demo() ;\n",
" delete d ;\n",
" delete d ; \n",
"}"
]
},
......@@ -351,7 +351,7 @@
"\n",
"By order of preference:\n",
"* `T&`: non-owner, cannot be null (always attached to an element).\n",
"* `gsl::gnot_null<T>`: non-owner, T is a pointer, cannot be zero.\n",
"* `gsl::not_null<T>`: non-owner, T is a pointer, cannot be zero.\n",
"* `T*`: non-owner, can be null, assumed to point to a unique element."
]
},
......
......@@ -46,7 +46,7 @@
"## New fixed-size `std::array`\n",
"\n",
"The GSL discuss two new types of arrays:\n",
"* `stack_array<T>`: an array allocated on the stack, with a fixed size, evaluable at compilation.\n",
"* `stack_array<T>`: an array allocated on the stack, with a fixed size, evaluable at compile time.\n",
"* `dyn_array<T>` : an array allocated on the heap, because its size is only known at runtime.\n",
"\n",
"Their elements are modifiable, unless the programmer instantiates the templates with a `const` type."
......@@ -113,7 +113,7 @@
"int main() {\n",
" std::array<int,5> arr = { 1, 2, 3, 4 } ;\n",
" for ( auto elem : arr )\n",
" std::cout<<elem<<\" \" ;\n",
" std::cout<<elem<<\" \" ; \n",
" std::cout<<std::endl ;\n",
"}"
]
......@@ -383,7 +383,7 @@
"source": [
"### Construction\n",
"\n",
"This type is the equivalent of a pointer and a size, and is based on the assumption that the **elements of the sequence are stored contiguously**. We can therefore only build a `gsl::span<T>` on top of arrays which comply with this memory contiguity, such as `std::vector<T>`, `std::array <T>`, `std::string` or an ordinary array `T[N]`."
"This type is the equivalent of a pointer and a size, and is based on the assumption that the **elements of the sequence are stored contiguously**. We can therefore only build a `gsl::span<T>` on top of arrays which comply with this memory contiguity, such as `std::vector<T>`, `std::array<T>`, `std::string` or an ordinary array `T[N]`."
]
},
{
......@@ -409,7 +409,7 @@
"#include <iostream>\n",
"#include <gsl/gsl>\n",
"\n",
"void display( gsl::span<int> data )\n",
"void display( gsl::span<const int> data )\n",
" {\n",
" for( auto e : data) std::cout << e << ' ';\n",
" std::cout << std::endl;\n",
......@@ -663,10 +663,11 @@
"%%file tmp.gsl-arrays.cpp\n",
"\n",
"#include \"tmp.gsl-arrays.h\"\n",
"#include <array>\n",
"\n",
"int main()\n",
" {\n",
" int arr[5] = { 1, 2, 3, 4, 5 } ;\n",
" std::array<int,5> arr = { 1, 2, 3, 4, 5 } ;\n",
" gsl::span<int,5> s{ arr } ;\n",
"\n",
" // run time\n",
......
......@@ -425,7 +425,7 @@
}
},
"source": [
"In addition, by going through a `string_view`, we lose the guarantee of having the terminal `0`. If the string must then be passed to other functions that expect the terminal `0`, it is better to use `string` frmo the beginning."
"In addition, by going through a `string_view`, we lose the guarantee of having the final `0`. If the string must then be passed to other functions that expect the terminal `0`, it is better to use `string` from the beginning."
]
},
{
......@@ -541,7 +541,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 32,
"metadata": {
"slideshow": {
"slide_type": "subslide"
......@@ -552,7 +552,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting tmp.gsl-utilities.cpp\n"
"Writing tmp.gsl-utilities.cpp\n"
]
}
],
......
......@@ -8,8 +8,8 @@ I times_power_of_two( I number, int exponent )
else { return (number<<exponent) ; }
}
template < typename I, std::enable_if_t<std::is_floating_point_v<I>> * = nullptr >
I times_power_of_two( I number, int exponent )
template < typename F, std::enable_if_t<std::is_floating_point_v<F>> * = nullptr >
F times_power_of_two( F number, int exponent )
{
while (exponent<0) { number /= 2 ; exponent++ ; }
while (exponent>0) { number *= 2 ; exponent-- ; }
......
#include <iostream>
#include <type_traits>
template < typename I>
I times_power_of_two( I number, int exponent )
template < typename T >
T times_power_of_two( TMP_MAX number, int exponent )
{
if constexpr (std::is_integral_v<I>)
if constexpr (std::is_integral_v<T>)
{
if (exponent<0) { return (number>>-exponent) ; }
else { return (number<<exponent) ; }
......
......@@ -32,7 +32,7 @@
}
},
"source": [
"## Make an `if` which depends on type\n",
"## Make an `if` which depends on some input type\n",
"\n",
"Have a look at the pseudo-code below, meant to move an iterator. We would like to support all the kinds of iterators, and benefit from a direct fast jump when this is a random access iterator."
]
......@@ -75,7 +75,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "subslide"
......@@ -83,7 +83,8 @@
},
"outputs": [],
"source": [
"#include<typeinfo>\n",
"#include <typeinfo>\n",
"\n",
"template< typename IterT, typename DistT >\n",
"void jump( IterT & iter, DistT d )\n",
" {\n",
......@@ -115,7 +116,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "subslide"
......@@ -130,7 +131,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "subslide"
......@@ -162,22 +163,13 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(incremental jump)\n",
"(direct jump)\n"
]
}
],
"outputs": [],
"source": [
"{\n",
" std::list<int> l = { 0, 1, 2, 3, 4 } ;\n",
......@@ -227,21 +219,13 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting tmp.tmp-introduction.cpp\n"
]
}
],
"outputs": [],
"source": [
"%%file tmp.legacy-tmp.cpp\n",
"\n",
......@@ -269,7 +253,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "-"
......@@ -282,22 +266,13 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"120\n",
"3628800\n"
]
}
],
"outputs": [],
"source": [
"!./tmp.legacy-tmp.exe"
]
......
......@@ -28,7 +28,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": null,
"metadata": {
"attributes": {
"classes": [
......@@ -40,28 +40,20 @@
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting tmp.types-handling.cpp\n"
]
}
],
"outputs": [],
"source": [
"%%file tmp.types-handling.cpp\n",
"\n",
"#include <iostream>\n",
"#include <typeinfo>\n",
"\n",
"template<typename U> // cas général \n",
"template<typename U> // general case\n",
"struct remove_possible_pointer\n",
" { typedef U type ; } ;\n",
" { using type = U ; } ;\n",
"\n",
"template<typename T> // pour U = T*\n",
"struct remove_possible_pointer<T*>\n",
" { typedef T type ; } ;\n",
" { using type = T ; } ;\n",
"\n",
"template<typename U>\n",
"const char * value_type_name( U /* variable */ )\n",
......@@ -76,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "-"
......@@ -89,22 +81,13 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"i\n",
"d\n"
]
}
],
"outputs": [],
"source": [
"!./tmp.types-handling.exe"
]
......@@ -122,7 +105,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": null,
"metadata": {
"attributes": {
"classes": [
......@@ -134,15 +117,7 @@
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting tmp.types-handling.h\n"
]
}
],
"outputs": [],
"source": [
"%%file tmp.types-handling.h\n",
"\n",
......@@ -160,7 +135,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": null,
"metadata": {
"attributes": {
"classes": [
......@@ -172,15 +147,7 @@
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting tmp.types-handling.cpp\n"
]
}
],
"outputs": [],
"source": [
"%%file tmp.types-handling.cpp\n",
"\n",
......@@ -202,7 +169,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "-"
......@@ -215,22 +182,13 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"i\n",
"d\n"
]
}
],
"outputs": [],
"source": [
"!./tmp.types-handling.exe"
]
......@@ -302,21 +260,13 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting tmp.types-handling.cpp\n"
]
}
],
"outputs": [],
"source": [
"%%file tmp.types-handling.cpp\n",
"\n",
......@@ -343,7 +293,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "-"
......@@ -356,22 +306,13 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"42 : false\n",
"3.14 : true\n"
]
}
],
"outputs": [],
"source": [
"!./tmp.types-handling.exe"
]
......@@ -389,21 +330,13 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting tmp.types-handling.cpp\n"
]
}
],
"outputs": [],
"source": [
"%%file tmp.types-handling.cpp\n",
"\n",
......@@ -433,7 +366,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "-"
......@@ -446,22 +379,13 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"42 : false\n",
"3.14 : true\n"
]
}
],
"outputs": [],
"source": [
"!./tmp.types-handling.exe"
]
......@@ -476,7 +400,7 @@
"source": [
"## In the standard library\n",
"\n",
"C++11 introduces a bunch of new traits, e.g. `is_fundamental<T>` (is T a predefined type ?), `is_array<T>` (is T an array?), and `is_base_of<T1, T2>` (is T2 a base class of T1, or T1 itself?). Note that these new traits are all implemented individually to facilitate their composition, rather than grouped together like the C++03 library."
"C++11 introduces a bunch of new traits, e.g. `is_fundamental<T>` (is T a builtin type ?), `is_array<T>` (is T an array?), and `is_base_of<T1, T2>` (is T2 a base class of T1, or T1 itself?). Note that these new traits are all implemented individually to facilitate their composition, rather than grouped together like the C++03 library."
]
},
{
......
......@@ -37,21 +37,13 @@
},
{
"cell_type": "code",
"execution_count": 65,
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting tmp.sfinae.cpp\n"
]
}
],
"outputs": [],
"source": [
"%%file tmp.sfinae.cpp\n",
"\n",
......@@ -76,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": 66,
"execution_count": null,
"metadata": {
"attributes": {
"classes": [
......@@ -95,7 +87,7 @@
},
{
"cell_type": "code",
"execution_count": 67,
"execution_count": null,
"metadata": {
"attributes": {