Is there a way to use C++ preprocessor stringification on variadic macro arguments? -
my guess answer question no, awesome if there way. clarify, assume have following macro:
#define my_variadic_macro(x...) // stuff here in macro definition
what somehow perform stringification on variables of x before passing variadic function; keyword here before. realize there's no way access individual arguments within macro definition, there way stringify arguments, maybe following?
#define my_variadic_macro(x...) some_variadic_function("some string", #x)
you can use various recursive macro techniques things variadic macros. example, can define num_args
macro counts number of arguments variadic macro:
#define _num_args(x100, x99, x98, x97, x96, x95, x94, x93, x92, x91, x90, x89, x88, x87, x86, x85, x84, x83, x82, x81, x80, x79, x78, x77, x76, x75, x74, x73, x72, x71, x70, x69, x68, x67, x66, x65, x64, x63, x62, x61, x60, x59, x58, x57, x56, x55, x54, x53, x52, x51, x50, x49, x48, x47, x46, x45, x44, x43, x42, x41, x40, x39, x38, x37, x36, x35, x34, x33, x32, x31, x30, x29, x28, x27, x26, x25, x24, x23, x22, x21, x20, x19, x18, x17, x16, x15, x14, x13, x12, x11, x10, x9, x8, x7, x6, x5, x4, x3, x2, x1, n, ...) n #define num_args(...) _num_args(__va_args__, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
then that, can write foreach
macro expands macro each element of list:
#define expand(x) x #define firstarg(x, ...) (x) #define restargs(x, ...) (__va_args__) #define foreach(macro, list) foreach_(num_args list, macro, list) #define foreach_(n, m, list) foreach__(n, m, list) #define foreach__(n, m, list) foreach_##n(m, list) #define foreach_1(m, list) m list #define foreach_2(m, list) expand(m firstarg list) foreach_1(m, restargs list) #define foreach_3(m, list) expand(m firstarg list) foreach_2(m, restargs list) :
which in turn allow to define macro stringifies each of arguments:
#define stringify(x) #x #define my_variadic_macro(...) foreach(stringify, (__va_args__))
Comments
Post a Comment