Skip to content

Instantly share code, notes, and snippets.

@afarah1
Created March 31, 2016 19:46
Show Gist options
  • Save afarah1/f345de004135c797001390da3dc33be1 to your computer and use it in GitHub Desktop.
Save afarah1/f345de004135c797001390da3dc33be1 to your computer and use it in GitHub Desktop.
Don't repeat yourself - Go-like parameters for C
/*
* Don't repeat yourself - Go-like parameters for C
*
* Before DRY:
*
* foo(int i, double x, double y, struct planet const *p1, struct planet const
* *p2, struct planet const *p3, int j)
*
* After DRY:
*
* foo(int i, DRY(double, x, y), DRY(struct planet const *, p1, p2, p3), int j)
*
* This version supports up to three parameters of the same type. It's simple
* to expand it to whatever amount of parameters you need.
*/
#define DRY1(type, e) type e
#define DRY2(type, e, ...) type e, DRY1(type, __VA_ARGS__)
#define DRY3(type, e, ...) type e, DRY2(type, __VA_ARGS__)
#define PP_NARG(...) PP_NARG_(__VA_ARGS__, PP_RSEQ_N())
#define PP_NARG_(...) PP_ARG_N(__VA_ARGS__)
#define PP_ARG_N(_1, _2, _3, N, ...) N
#define PP_RSEQ_N() 3, 2, 1, 0
#define CAT2(a, b) a ## b
#define CAT(a, b) CAT2(a, b)
#define DRY(type, ...) CAT(DRY, PP_NARG(__VA_ARGS__))(type, __VA_ARGS__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment