c++ - Array Initialization Macro -
i trying come macro following,
my_macro(type,name,args...) my_macro(int,_array,1,2,3)
and expand to,
int _array[] = {1,2,3}; coll* __array = my_call(&array[0],&array[1],&array[2]);
is possible with/without compiler specific magic?
my_call expects variable number of arguments , not want pass array directly.
edit: using accepted answer following question var args,
macro returning number of arguments given in c?
so can find how many elements in array.
you can start (c99, not gcc-specific):
#define my_macro(type, name, ...) \ type name[] = {__va_args__};
the my_call
part more difficult; there macros online can count number of arguments, need boost.preprocessor (which should work in c) apply my_call
consecutive elements of array. how many elements have maximum? might want like:
#define count(...) count2(__va_args__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) #define count2(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, count, ...) count #define pp_cat(a, b) pp_cat2(a, b) #define pp_cat2(a, b) ## b #define call_my_func(arr, ...) my_call(pp_cat(iter_, count(__va_args__))(arr)); #define iter_0(arr) /**/ #define iter_1(arr) (arr) #define iter_2(arr) (arr), iter_1((arr) + 1) #define iter_3(arr) (arr), iter_2((arr) + 1) #define iter_4(arr) (arr), iter_3((arr) + 1)
and on.
Comments
Post a Comment