Update 105-macros.md

Added an example of dangerous macroses. The example is taken from Bjarne Stroustrup's C++11 4th Edition book, §12.6.
pull/8296/head
SaintFTS 2 months ago committed by GitHub
parent cbfd4f7fcb
commit 7d7d324454
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 10
      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.
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.

Loading…
Cancel
Save