Quick Links: |
repeat a macro invocation with different numeric arguments. More...
BSLS_MACROREPEAT(N, MACRO) | Invoke MACRO(1) MACRO(2) ... MACRO(N) |
BSLS_MACROREPEAT_COMMA(N, MACRO) | N comma-separated invocations of MACRO |
BSLS_MACROREPEAT_SEP(N, MACRO, S) | N invocations of MACRO separated by S |
BSLS_MACROREPEAT*
macro. For example: #define FOO(n) foo ## n doit(BSLS_MACROREPEAT_COMMA(5, FOO));
doit(foo1, foo2, foo3, foo4, foo5);
foo<V>
that adds the (compile-time) value V
to a global total
each time it is called: int total = 0; template <int V> void foo() { total += V; }
foo<X>()
once for each X
in the range 2
to 6
. To do that, we create a macro, FOO_STMNT(X)
which and calls foo<X+1>
(i.e., FOO_STMNT(1)
will call foo<2>()
). Then we invoke FOO_STMNT
5 times with arguments 1, 2, 3, 4, and 5 using the BSLS_MACROREPEAT
macro: int main() { #define FOO_STMNT(X) foo<X+1>(); // Semicolon at end of each statement BSLS_MACROREPEAT(5, FOO_STMNT) assert(20 == total); return 0; }
BSLS_MACROREPEAT_COMMA
. First, assume a function, fmtQuartet
that takes four integer arguments and formats them into a string: #include <cstring> #include <cstdio> void fmtQuartet(char *result, int a, int b, int c, int d) { std::sprintf(result, "%d %d %d %d", a, b, c, d); }
X(x)
that ignores its argument and simply expands to an unchanging set of tokens. If the repeated argument is named i
, then the expansion of X(x)
is simply (i)
: int main() { char buffer[20]; int i = 8; #define X(x) (i)
X(x)
three times within the argument list of fmtQuart
. We use BSLS_MACROREPEAT_COMMA
for these invocations, as it inserts a comma between each repetition: fmtQuartet(buffer, "%d %d %d %d", 7, BSLS_MACROREPEAT_COMMA(3, X)); assert(0 == std::strcmp(buffer, "7 8 8 8")); return 0; }
BITVAL
that computes the value of a single bit B
in the mask: #define BITVAL(B) (1 << (B - 1))
BSLS_MACROREPEAT_SEP
to invoke BITVAL
7 times, separating the repetitions with the bitwise OR operator: const unsigned mask = BSLS_MACROREPEAT_SEP(7, BITVAL, |); int main() { assert(127 == mask); return 0; }