I’m trying to create a simple macro using varargs, but I’m getting “va_start used in function with fixed args” and I don’t understand why and where I’m wrong. The code is the following:
#define FOO(obj, ...) \
va_list args; \
va_start(args, obj); \
NSString *currentObject; \
while ((currentObject = va_arg(args, NSString*)) != nil) { \
NSLog(@"string: %@", currentObject); \
} \
va_end(args);
The
va_listtype and its associated operations are for variadic functions. They don’t work for variadic macros, which use__VA_ARGS__and work somewhat differently.In this example I can’t see any reason to use a macro; you should probably just use a function instead. If it turns out that you do need a macro, you will probably end up having to pass
__VA_ARGS__as the arguments to a variadic function, which can then unpack the arguments and perform further work.