Skip to content

Instantly share code, notes, and snippets.

@silentbicycle
Created March 30, 2012 14:28
Show Gist options
  • Save silentbicycle/2251910 to your computer and use it in GitHub Desktop.
Save silentbicycle/2251910 to your computer and use it in GitHub Desktop.
macro capture example
#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