diff --git a/1-ProceduralProgramming/4-Functions.ipynb b/1-ProceduralProgramming/4-Functions.ipynb index 47fbfa70b6a861761f36c0d1c0b4ee91855c080c..18386df91dff21174f3625fead51d30599a2fa2a 100644 --- a/1-ProceduralProgramming/4-Functions.ipynb +++ b/1-ProceduralProgramming/4-Functions.ipynb @@ -1405,6 +1405,160 @@ "We will deal with main functions later when we will work in a true C++ environment." ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unused arguments\n", + "\n", + "It might happen that a function provides parameters which are not actually used in the implementation; if you've set up the appropriate warning flags for your compiler, it should warn you about these unused parameters:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%%cppmagics --print_command clang\n", + "\n", + "#include <cstdlib>\n", + "\n", + "int main(int argc, char** argv)\n", + "{\n", + "\n", + " return EXIT_SUCCESS;\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To counteract this you have three strategies:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Not naming the parameters" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%%cppmagics --print_command clang\n", + "\n", + "#include <cstdlib>\n", + "\n", + "int main(int , char** )\n", + "{\n", + "\n", + " return EXIT_SUCCESS;\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Casting the parameters to void" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%%cppmagics --print_command clang\n", + "\n", + "#include <cstdlib>\n", + "\n", + "int main(int argc, char** argv)\n", + "{\n", + " static_cast<void>(argc);\n", + " static_cast<void>(argv);\n", + "\n", + " return EXIT_SUCCESS;\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Using [[maybe_unused]] (since C++ 17)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%%cppmagics --print_command clang\n", + "\n", + "#include <cstdlib>\n", + "\n", + "int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)\n", + "{\n", + " return EXIT_SUCCESS;\n", + "}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We strongly advised you to use the latest, if your code is using at least C++ 17:\n", + "\n", + "- Not naming the parameters might seem innocuous here, but in more meaningful codes you lose expressivity: the fact that the argument isn't used doesn't mean you don't want the intel about what it is about (we'll see later in object programming `override` [we'll see later](../2-ObjectProgramming/7-polymorphism.ipynb#override-keyword)). Naming bears information, and dropping it is therefore loss of information.\n", + "- Casting to `void` is not straightforward for developers who don't know the idiom. Even so, the fact that it's not directly indicated at the declaration site makes it less expressive.\n", + "\n", + "`[[maybe_unused]]` may in fact be used as well for local variables:\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%%cppmagics --print_command clang\n", + "\n", + "#include <cstdlib>\n", + "\n", + "int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)\n", + "{\n", + " int a;\n", + " \n", + " return EXIT_SUCCESS;\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "%%cppmagics --print_command clang\n", + "\n", + "#include <cstdlib>\n", + "\n", + "int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)\n", + "{\n", + " [[maybe_unused]] int a; // fixes the warning\n", + " \n", + " return EXIT_SUCCESS;\n", + "}" + ] + }, { "cell_type": "markdown", "metadata": {},