Skip to content

Instantly share code, notes, and snippets.

@benjamn
Created February 11, 2009 21:33
Show Gist options
  • Save benjamn/62280 to your computer and use it in GitHub Desktop.
Save benjamn/62280 to your computer and use it in GitHub Desktop.
/**
** Macro: CS{0,1,2,...,15}({COMMA,NOCOMMA},P)
* ===========================================
* This integer-parameterized comma-separated list-generating pseudo-recursive
* macro supports lists of up to 15 elements (and as few as 0), with and without
* initial commas. Examples:
*
* CS5(NOCOMMA, P) => P1, P2, P3, P4, P5
* CS3(COMMA, typename T) => , typename T1, typename T2, typename T3
* CS0(NOCOMMA, T) =>
* CS0(COMMA, T) =>
* CS1(NOCOMMA, T) => T1
* CS1(COMMA, T) => , T1
*
* This macro is chiefly intended as an implementation aid to other macros:
* recall that the name of a macro can be composed from a prefix and an integer
* using the ## operator:
*
* #define TPARAMS(N) template <class C CS##N(COMMA, typename T)>
*
* In this example, TPARAMS(4) would expand to
*
* template <class C, typename T1, typename T2, typename T3, typename T4>
*
* and TPARAMS(0) would expand to
*
* template <class C>
*
* Final commas may not be illegal, but they're best to avoid. The COMMA_CS1
* and NOCOMMA_CS1 macros are not intended to be used directly. Need longer
* lists? I think you know what to do.
*/
// Base cases for the pseudo-recursion:
#define CS0(C,P)
#define COMMA_CS1(C,P) CS0(C,P), P##1
#define NOCOMMA_CS1(C,P) CS0(C,P) P##1
#define CS1(C,P) C##_CS1(C,P)
#define CS2(C,P) CS1(C,P), P##2
#define CS3(C,P) CS2(C,P), P##3
#define CS4(C,P) CS3(C,P), P##4
#define CS5(C,P) CS4(C,P), P##5
#define CS6(C,P) CS5(C,P), P##6
#define CS7(C,P) CS6(C,P), P##7
#define CS8(C,P) CS7(C,P), P##8
#define CS9(C,P) CS8(C,P), P##9
#define CS10(C,P) CS9(C,P), P##10
#define CS11(C,P) CS10(C,P), P##11
#define CS12(C,P) CS11(C,P), P##12
#define CS13(C,P) CS12(C,P), P##13
#define CS14(C,P) CS13(C,P), P##14
#define CS15(C,P) CS14(C,P), P##15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment