From 0f5771bcc24465903ca1e267e4b3e987916212f2 Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent <vincent.rouvreau@inria.fr> Date: Thu, 6 May 2021 16:15:59 +0200 Subject: [PATCH 01/10] Remove semicolon at the end of function declaration, and explains why it is required to add a semicolon at the end of a struct --- 2-ObjectProgramming/1-Introduction.ipynb | 37 +++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/2-ObjectProgramming/1-Introduction.ipynb b/2-ObjectProgramming/1-Introduction.ipynb index 735c557..23da4eb 100644 --- a/2-ObjectProgramming/1-Introduction.ipynb +++ b/2-ObjectProgramming/1-Introduction.ipynb @@ -43,7 +43,7 @@ "double norm(double v_x, double v_y, double v_z) \n", "{ \n", " return std::sqrt( v_x * v_x + v_y * v_y + v_z * v_z ); \n", - "};\n", + "}\n", "\n", "{\n", " double v1_x, v1_y, v1_z;\n", @@ -158,6 +158,41 @@ "Let's also highlight the `.` syntax which allows to access the attributes of an object (e.g `v1.x`).\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### The semicolon at the end of a `struct`\n", + "This comes historically from the C, where a `struct` is considered as a type declaration and it allows to do:\n", + "\n", + "**Xeus-cling issue:** Here cling doesn't manage to compile it but it is accepted rightfully by a full-fledged compiler (see for instance [@Coliru](http://coliru.stacked-crooked.com/a/3b77606ea8082485)):" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "struct Vector\n", + "{\n", + " double x;\n", + " double y;\n", + " double z; \n", + "} v1; // Here the struct is declared and at the same time an object v1 is created\n", + "\n", + "v1.x = 1.;\n", + "v1.y = 5.;\n", + "v1.z = -2.;" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is the reason why a semicolon is always required at the end of a `struct` or a `class` declaration." + ] + }, { "cell_type": "markdown", "metadata": {}, -- GitLab From 6048b4547cc3b9a73321678bc0fb515fa5601b86 Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent <vincent.rouvreau@inria.fr> Date: Fri, 7 May 2021 09:17:28 +0200 Subject: [PATCH 02/10] Typo --- 1-ProceduralProgramming/3-Types.ipynb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/1-ProceduralProgramming/3-Types.ipynb b/1-ProceduralProgramming/3-Types.ipynb index 8592b58..958f435 100644 --- a/1-ProceduralProgramming/3-Types.ipynb +++ b/1-ProceduralProgramming/3-Types.ipynb @@ -41,7 +41,7 @@ "bool undefined; // UNDEFINED !! \n", "if (undefined)\n", " std::cout << \"This text might appear or not - it's truly undefined and may vary from \"\n", - " \"one run/compiler/architecture/etc... to another!\" << std::endl;`" + " \"one run/compiler/architecture/etc... to another!\" << std::endl;" ] }, { @@ -438,7 +438,6 @@ "source": [ "{\n", " float f = 1.12345678901234567890;\n", - " double d = 2.12345678901234567890;\n", " double d_f { f }; // OK\n", "}" ] -- GitLab From 5a9ce1068060dced99d71582e674f77e7a7f97fc Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent <vincent.rouvreau@inria.fr> Date: Fri, 7 May 2021 10:06:21 +0200 Subject: [PATCH 03/10] suffix with an underscore attributes --- 2-ObjectProgramming/3-constructors-destructor.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/2-ObjectProgramming/3-constructors-destructor.ipynb b/2-ObjectProgramming/3-constructors-destructor.ipynb index e74207f..be00709 100644 --- a/2-ObjectProgramming/3-constructors-destructor.ipynb +++ b/2-ObjectProgramming/3-constructors-destructor.ipynb @@ -478,7 +478,7 @@ "source": [ "struct BadlyInitialized\n", "{\n", - " int a;\n", + " int a_;\n", "};" ] }, @@ -492,7 +492,7 @@ "\n", "{\n", " BadlyInitialized my_object;\n", - " std::cout << \"Undefined behaviour: no guarantee for the value of the data attribute!: \" << my_object.a << std::endl;\n", + " std::cout << \"Undefined behaviour: no guarantee for the value of the data attribute!: \" << my_object.a_ << std::endl;\n", "}" ] }, -- GitLab From a6a498397726c8822dd0549640c2f8a73af87f55 Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent <vincent.rouvreau@inria.fr> Date: Fri, 7 May 2021 11:36:43 +0200 Subject: [PATCH 04/10] Add a warning about underscore prefixed attributes --- 2-ObjectProgramming/2-Member-functions.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2-ObjectProgramming/2-Member-functions.ipynb b/2-ObjectProgramming/2-Member-functions.ipynb index 4aabce6..d4be615 100644 --- a/2-ObjectProgramming/2-Member-functions.ipynb +++ b/2-ObjectProgramming/2-Member-functions.ipynb @@ -97,7 +97,7 @@ "\n", "In most cases, it might be altogether removed; we have to put it explicitly here solely because we named the `init` parameters with the same name as the data attribute. If not, we could have avoided to mention it completely.\n", "\n", - "An usual convention is to suffix data attributes with a `_`; doing so remove the need to the explicit `this`:" + "An usual convention is to suffix data attributes with a `_` (be carefull, attributes prefixed with a `_` is reserved to the ; doing so remove the need to the explicit `this`:" ] }, { -- GitLab From b42baf0d441cc186ec1baf2397d51a95ec687e3f Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent <vincent.rouvreau@inria.fr> Date: Fri, 7 May 2021 11:47:43 +0200 Subject: [PATCH 05/10] Add a warning about underscore prefixed attributes --- 2-ObjectProgramming/2-Member-functions.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2-ObjectProgramming/2-Member-functions.ipynb b/2-ObjectProgramming/2-Member-functions.ipynb index d4be615..0751064 100644 --- a/2-ObjectProgramming/2-Member-functions.ipynb +++ b/2-ObjectProgramming/2-Member-functions.ipynb @@ -97,7 +97,7 @@ "\n", "In most cases, it might be altogether removed; we have to put it explicitly here solely because we named the `init` parameters with the same name as the data attribute. If not, we could have avoided to mention it completely.\n", "\n", - "An usual convention is to suffix data attributes with a `_` (be carefull, attributes prefixed with a `_` is reserved to the ; doing so remove the need to the explicit `this`:" + "An usual convention is to suffix data attributes with a `_` (**be carefull**, attributes prefixed with a `_` is reserved by the C++ standard); doing so remove the need to the explicit `this`:" ] }, { -- GitLab From 7e94dbdbb87cdf7480d1d623cb22e974b50b7f9c Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent <vincent.rouvreau@inria.fr> Date: Fri, 7 May 2021 11:51:04 +0200 Subject: [PATCH 06/10] Use braces for default attributes values (as explained in the previous notebook) --- 2-ObjectProgramming/4-encapsulation.ipynb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/2-ObjectProgramming/4-encapsulation.ipynb b/2-ObjectProgramming/4-encapsulation.ipynb index f4f6ed1..93c4f06 100644 --- a/2-ObjectProgramming/4-encapsulation.ipynb +++ b/2-ObjectProgramming/4-encapsulation.ipynb @@ -93,7 +93,7 @@ " \n", " private:\n", " \n", - " int a_ = -9999999; // stupid default value.\n", + " int a_ { -9999999 }; // stupid default value.\n", " \n", " void SetValue(int a);\n", " \n", @@ -458,8 +458,8 @@ "\n", " private:\n", " \n", - " double length_ = -1.e20; // a stupid value which at least is deterministically known...\n", - " double width_ = -1.e20;\n", + " double length_ { -1.e20 }; // a stupid value which at least is deterministically known...\n", + " double width_ { -1.e20 };\n", "};" ] }, @@ -591,9 +591,9 @@ " \n", " private:\n", " \n", - " double area_ = -1.e20; // a stupid value which at least is deterministic...\n", - " double length_ = -1.20; \n", - " double width_ = -1.20;\n", + " double area_ { -1.e20 }; // a stupid value which at least is deterministic...\n", + " double length_ { -1.20 }; \n", + " double width_ { -1.20 };\n", "};" ] }, -- GitLab From 733a5349159240759a565e2b301f94faa00363e8 Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent <vincent.rouvreau@inria.fr> Date: Fri, 7 May 2021 12:25:23 +0200 Subject: [PATCH 07/10] rename parashift isocpp --- 2-ObjectProgramming/5-static.ipynb | 26 ++++++++++++++++---------- bibliography.ipynb | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/2-ObjectProgramming/5-static.ipynb b/2-ObjectProgramming/5-static.ipynb index 91722c3..2ca0f1a 100644 --- a/2-ObjectProgramming/5-static.ipynb +++ b/2-ObjectProgramming/5-static.ipynb @@ -75,9 +75,15 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "It might be used for instance if you need to initialize something on the very first call of a function:\n", - "\n", - "````\n", + "It might be used for instance if you need to initialize something on the very first call of a function:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "void FunctionWithStatic()\n", "{\n", " static bool is_first_call = true;\n", @@ -85,14 +91,12 @@ " if (is_first_call)\n", " {\n", " // Init stuff here on first call only\n", - " ... \n", + " // ... \n", " is_first_call = false;\n", " }\n", " \n", - " ... // code executed at each call\n", - "}\n", - "\n", - "````" + " // ... code executed at each call\n", + "}" ] }, { @@ -239,8 +243,10 @@ ] }, { - "cell_type": "raw", + "cell_type": "code", + "execution_count": null, "metadata": {}, + "outputs": [], "source": [ "#include <iostream>\n", "#include <vector>\n", @@ -400,7 +406,7 @@ "To understand better the possible issue and the fix proposed, you may have a look at:\n", "\n", "* Item 26 of \\cite{Meyers1995}\n", - "* The dedicated item on [Parashift FAQ](https://isocpp.org/wiki/faq/ctors#static-init-order)\n" + "* The dedicated item on [isocpp FAQ](https://isocpp.org/wiki/faq/ctors#static-init-order)\n" ] }, { diff --git a/bibliography.ipynb b/bibliography.ipynb index 2dae2a0..5fda224 100644 --- a/bibliography.ipynb +++ b/bibliography.ipynb @@ -77,7 +77,7 @@ "\n", "Provides a dictionary entry with the API explained for every functions/algorithms/class/you name it from the language and the standard library. Usually you will find them upfront if you Google a term from C++ (e.g. `std::vector`). Explanation might be a bit terse, but there is now in most cases an example powered by Coliru which present a concrete (albeit simple - but you're accustomed to that if you followed this tutorial!) use case.\n", "\n", - "### [Parashift](https://isocpp.org/faq)\n", + "### [isocpp](https://isocpp.org/faq)\n", "\n", "A gigantic FAQ that covers many aspects of C++; this site is in fact a merge from two previously independant FAQ, one of which was maintained by Bjarne Stroustrup, creator of the language.\n", "\n", -- GitLab From 9f27f35bc435e79f70dfe08a9935d306f4371bc5 Mon Sep 17 00:00:00 2001 From: ROUVREAU Vincent <vincent.rouvreau@inria.fr> Date: Fri, 7 May 2021 16:38:37 +0200 Subject: [PATCH 08/10] Use braces for default attributes values (as explained in the previous notebook) and typos --- 2-ObjectProgramming/6-inheritance.ipynb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/2-ObjectProgramming/6-inheritance.ipynb b/2-ObjectProgramming/6-inheritance.ipynb index 1e9f6dc..5c962d9 100644 --- a/2-ObjectProgramming/6-inheritance.ipynb +++ b/2-ObjectProgramming/6-inheritance.ipynb @@ -132,7 +132,7 @@ "\n", "### Multiple layer of inheritance\n", "\n", - "A child class may also be the parent of another class (unless `final` is used - see later)\n", + "A child class may also be the parent of another class (unless [`final`](6-inheritance.ipynb#final-keyword) is used - see later)\n", "\n" ] }, @@ -347,9 +347,9 @@ " \n", " private:\n", " \n", - " double width_ = -1.e20; // Stupid value that would play havoc if not properly initialized\n", + " double width_ { -1.e20 }; // Stupid value that would play havoc if not properly initialized\n", " // - std::optional (C++17) would probably a better choice.\n", - " double length_ = -1.e20; \n", + " double length_ { -1.e20 }; \n", "};" ] }, @@ -1847,7 +1847,7 @@ "source": [ "So you could devise a way to identify which is the dynamic type of your `PolymorphicVehicle` pointer and cast it dynamically to the rightful type so that extended API offered by the derived class is accessible.\n", "\n", - "If you find this clunky, you are not alone: by experience if you really need to resort to **dynamic_cast** it's probably your data architecture needs some revision. But maybe the mileage vary for other developers, and it's useful to know the possibility exist." + "If you find this clunky, you are not alone: by experience if you really need to resort to **dynamic_cast** it's probably your data architecture needs some revision. But maybe the mileage vary for other developers, and it's useful to know the possibility exists." ] }, { @@ -1856,7 +1856,7 @@ "source": [ "## `final` keyword\n", "\n", - "If you need to specify a class can't be derived, you may stick a `final` keyword in its declaration (from C++11 onward):\n" + "If you need to specify a class that can't be derived, you may stick a `final` keyword in its declaration (from C++11 onward):\n" ] }, { @@ -2100,7 +2100,7 @@ "source": [ "struct DerivedClass4 : public BaseClass4\n", "{\n", - " DerivedClass4() = default;; \n", + " DerivedClass4() = default; \n", " \n", " virtual std::string ClassName() const;\n", "};" @@ -2192,7 +2192,7 @@ "source": [ "struct DerivedClass5 : public BaseClass5\n", "{\n", - " DerivedClass5() = default;; \n", + " DerivedClass5() = default; \n", " \n", " virtual std::string ClassName() const;\n", "};" -- GitLab From 6da32fb55ba5da211ccfbbc769743694cefd6c08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gilles?= <sebastien.gilles@inria.fr> Date: Fri, 7 May 2021 17:24:04 +0200 Subject: [PATCH 09/10] Reformulate the paragraph about ; at end of struct. --- 2-ObjectProgramming/1-Introduction.ipynb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/2-ObjectProgramming/1-Introduction.ipynb b/2-ObjectProgramming/1-Introduction.ipynb index 23da4eb..0a972ce 100644 --- a/2-ObjectProgramming/1-Introduction.ipynb +++ b/2-ObjectProgramming/1-Introduction.ipynb @@ -163,9 +163,7 @@ "metadata": {}, "source": [ "### The semicolon at the end of a `struct`\n", - "This comes historically from the C, where a `struct` is considered as a type declaration and it allows to do:\n", - "\n", - "**Xeus-cling issue:** Here cling doesn't manage to compile it but it is accepted rightfully by a full-fledged compiler (see for instance [@Coliru](http://coliru.stacked-crooked.com/a/3b77606ea8082485)):" + "This comes historically from the C, where a `struct` could be defined and initialized at the same time (or should - Xeus-cling doesn't manage it... As usual you may check a full-fledged compiler accepts it [@Coliru](http://coliru.stacked-crooked.com/a/3b77606ea8082485)):" ] }, { @@ -174,6 +172,8 @@ "metadata": {}, "outputs": [], "source": [ + "// Xeus-cling issue (at least circa May 2021)\n", + "\n", "struct Vector\n", "{\n", " double x;\n", @@ -190,7 +190,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "This is the reason why a semicolon is always required at the end of a `struct` or a `class` declaration." + "This is absolutely **not** encouraged in C++, but it may help you to remember always closing a `struct` (or later a `class`) with a semicolon." ] }, { -- GitLab From f478f350449efe0ba23f1ff5b6828104c8c87c81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gilles?= <sebastien.gilles@inria.fr> Date: Fri, 7 May 2021 17:24:17 +0200 Subject: [PATCH 10/10] Typo. --- 2-ObjectProgramming/2-Member-functions.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2-ObjectProgramming/2-Member-functions.ipynb b/2-ObjectProgramming/2-Member-functions.ipynb index 0751064..f561176 100644 --- a/2-ObjectProgramming/2-Member-functions.ipynb +++ b/2-ObjectProgramming/2-Member-functions.ipynb @@ -97,7 +97,7 @@ "\n", "In most cases, it might be altogether removed; we have to put it explicitly here solely because we named the `init` parameters with the same name as the data attribute. If not, we could have avoided to mention it completely.\n", "\n", - "An usual convention is to suffix data attributes with a `_` (**be carefull**, attributes prefixed with a `_` is reserved by the C++ standard); doing so remove the need to the explicit `this`:" + "An usual convention is to suffix data attributes with a `_` (**be careful**, attributes prefixed with a `_` is reserved by the C++ standard); doing so remove the need to the explicit `this`:" ] }, { -- GitLab