Created
March 30, 2012 14:28
-
-
Save silentbicycle/2251910 to your computer and use it in GitHub Desktop.
macro capture example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define CONTRIVED_IF_EXAMPLE(F, COUNTER) F(COUNTER); COUNTER++; | |
if (some_condition) CONTRIVED_IF_EXAMPLE(func, x); | |
/* This will expand to: */ | |
if (some_condition) func(x); x++; | |
/* Note that the `x++;` statement will always evaluate - it is outside the `if`. Defining the macro with braces will fix the issue. */ | |
#define FIXED_IF_EXAMPLE(F, COUNTER) { F(COUNTER); COUNTER++; } | |
/* Similarly, compare */ | |
#define CONTRIVED_EXAMPLE(X, Y) X + Y | |
uint32_t val = 3*CONTRIVED_EXAMPLE(12, 345); /* */ | |
/* to */ | |
#define CONTRIVED_EXAMPLE(X, Y) (X + Y) | |
uint32_t val = 3*CONTRIVED_EXAMPLE(12, 345); /* */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment