Macros for writing macros#

Create a string literal#

LITERAL_STRINGIZE(TEXT)#

Evaluates as a string literal containing TEXT, without performing macro expansion.

Example

LITERAL_STRINGIZE(__LINE__)
Evaluates to:
"__LINE__"
Since

0.2

Parameters:
  • TEXT – doesn’t even have to be valid C !

STRINGIZE(EXPRESSION)#

Evaluates as a string literal containing EXPRESSION with its macros expanded.

Example

STRINGIZE(__LINE__)
May evaluate to:
"21"
Since

0.2

Parameters:
  • EXPRESSION – doesn’t even have to be valid C !

Combine tokens#

LITERAL_CONCAT(TEXT1, TEXT2)#

Evaluates as a single identifier token, that is TEXT1 concatenated with TEXT2, not evaluating any macro inside.

Example

LITERAL_CONCAT(temp, __COUNTER__)
Evaluates to:
temp__COUNTER__
Since

0.2

CONCAT(EXPRESSION1, EXPRESSION2)#

Evaluates as a single identifier token, that is the evaluation of EXPRESSION1 concatenated with the evaluation of EXPRESSION2.

Example

CONCAT(temp, __COUNTER__)
May evaluate to:
temp0
Since

0.2

Count arguments#

ARG_COUNT(...)#

Evaluates to an integer literal representing the number of arguments it was provided.

The limit is 80 arguments.

Example#

ARG_COUNT("A", 'B', C)
Evaluates to:
3
Since

0.2

Fold#

Apply a binary macro on any number of argument.

Configuration

FOLD#

Default fold to use, can be overridden.

Defines

TREE_FOLD(M, ...)#

Apply the binary macro M on pairs of inputs, in a tree-like fashion.

Example

TREE_FOLD(F, A, B, C, D)
expands to:
F(F(A, B), F(C, D))
Since

0.2

FOLD_RIGHT(M, ...)#

Apply the binary macro M on pairs of inputs, first grouping the right-most arguments.

Example

FOLD_RIGHT(F, A, B, C, D)
expands to:
F(A, F(B, F(C, D)))
Since

0.2

Iteration#

Generate code by calling a macro repeatedly.

Defines

EACH(...)#

To be used with FOR.

FOR(LIST, MACRO, ...)#

Call MACRO on each item of the LIST, forwarding extra arguments if any.

Example

FOR(EACH(A, B, C), F, X, Y)
will expand to:
F(X, Y, A)
F(X, Y, B)
F(X, Y, C)
Since

0.2

Parameters:
  • LIST – shall be of the form EACH(P0, P1, ..., Pn-1)

  • MACRO – shall be a function-like macro

Manipulate pairs#

Manipulate pairs.

Defines

PAIR_FIRST(A, B)#

Extract the first element of a pair.

PAIR_LAST(A, B)#

Extract the second element of a pair.

PAIR_FLATTEN(A, B)#

Expand to both elements of a pair following each other.

PAIR_ASSIGN(A, B)#

Expands to an assignation.

Misc.#

Defines

IDENTITY(...)#

Expands to its arguments.

ADD_COMMA(F, ...)#

Apply F to the rest of the arguments, adding a comma at the end.

ADD_COMMA_FLAT(A, B)#