#define INITIALIZE_INT_ARRAY(elem_type, array_name,...) \
elem_type array_name[] = { __VA_ARGS__ }; \
INITIALIZE_INT_ARRAY(int, arr, 1, 2, 3, 4, 5)
// will expand to
int arr[] = {1, 2, 3, 4. 5};
Now I want to support tuples in __VA_ARGS__ and will simply take the first element of the tuple if it is a tuple.
INITIALIZE_INT_ARRAY(int, arr, 1, (2, hello), (3, world), (4, X), 5)
// will still expand to
int arr[] = {1, 2, 3, 4, 5}
How can I change my INITIALIZE_INT_ARRAY?
In general, iterating over comma-separate lists in preprocessor requires writing
O(n)boilerplate macros. You can write yourself or get them from Boost.Preprocessor, or...You can use a different syntax for the list:
FOO(int, arr, (1)(2, hello)(3, world)(4, X)(5)).Then the macro can be written like this, without boilerplate: