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<
 ... ... @@ -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 numbers = { 1, 2, 3, 4, 5 } ;\n", "auto mult = make_multiplier(3) ;\n", "std::for_each(numbers.begin(),numbers.end(),mult) ;" "{\n", " std::vector numbers = { 1, 2, 3, 4, 5 } ;\n", " auto mult = make_multiplier(3) ;\n", " std::for_each(numbers.begin(),numbers.end(),mult) ;\n", "}" ] }, { ... ...
 #include #include template class my_span { public : template 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 void print( my_span data ) { for( std::size_t i = 0 ; i < data.size() ; ++i ) std::cout << data[i] << ' ' ; std::cout << std::endl ; } int main() { std::array arr = { 1, 2, 3, 4, 5 } ; my_span s { arr } ; print(s) ; return 0 ; }
 #include #include #include class Demo { public: Demo() { std::cout<<"Constructor"<, int> * = nullptr> using my_owner = T ; template , 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 d1 ; // COMPILATION ERROR: Demo is not a pointer //my_not_null p1 ; // COMPILATION ERROR: p1 is not initialized //my_not_null p2(nullptr) ; // COMPILATION ERROR: p2 cannot be null my_owner d2 = new Demo() ; my_not_null p3 = d2 ; //p3 = nullptr ; // EXECUTION ERROR: p3 cannot be null p3->print() ; delete d2 ; }
 #include #include template struct no_narrow { TypeOut data_ ; template no_narrow( TypeIn data ) : data_(data) { assert(static_cast(data)==data) ; } operator TypeOut() { return data_ ; } } ; int main() { double d1 = 42 ; int i1 = no_narrow(d1) ; std::cout<(d2) ; std::cout<
 #include template struct FinalAction { Function f_ ; FinalAction( Function f ) : f_(f) {} ~FinalAction() { f_() ; } } ; template auto my_finally( Function f ) { return FinalAction(f) ; } int main() { std::cout<<"Step 1"<