From 7d7d324454f3081c3112163067d331e38ac21755 Mon Sep 17 00:00:00 2001 From: SaintFTS <152052965+SaintFTS@users.noreply.github.com> Date: Mon, 3 Mar 2025 23:10:21 +0500 Subject: [PATCH] Update 105-macros.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added an example of dangerous macroses. The example is taken from Bjarne Stroustrup's C++11 4th Edition book, ยง12.6. --- .../cpp/content/109-language-concepts/105-macros.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/data/roadmaps/cpp/content/109-language-concepts/105-macros.md b/src/data/roadmaps/cpp/content/109-language-concepts/105-macros.md index f607bba49..e501b543b 100644 --- a/src/data/roadmaps/cpp/content/109-language-concepts/105-macros.md +++ b/src/data/roadmaps/cpp/content/109-language-concepts/105-macros.md @@ -35,6 +35,14 @@ This macro defines a function-like macro `SQUARE` that calculates the square of ```cpp int square_of_five = SQUARE(5); // expands to ((5) * (5)) ``` +**CAUTION!** + +The following macro may seem to be the same as the one above, but this version can lead to unexpected results: +```cpp +#define SQUARE(x) (x*x); // DANGEROUS!!! +#define PI 3.14159; +const double val = SQUARE(PI+2); // expands to (PI+2*PI+2), and equals to (3*PI+2) +``` ## Conditional Compilation @@ -52,4 +60,4 @@ Example: #endif ``` -This example demonstrates how you can use macros to control the parts of code that are being compiled, depending on the presence or absence of a macro definition. \ No newline at end of file +This example demonstrates how you can use macros to control the parts of code that are being compiled, depending on the presence or absence of a macro definition.