Question
I am looking for a variadic C preprocessor macro that passes its argument and a corresponding format string to a function, repeating a character depending on the number of arguments.
For example, I would like a macro FOO
which expands as follows (or to equivalent C code):
FOO(1)
→bar("d",1)
FOO(1,2)
→bar("dd",1,2)
,FOO(1,2,3)
→bar("ddd",1,2,3)
- bonus:
FOO()
→bar("")
While I can combine the solutions to C preprocessor macro for returning a string repeated a certain number of times and C++ preprocessor __VA_ARGS__ number of arguments (or similar questions) or use variadic macros, these have several drawbacks such as:
- requiring special libraries, such as Boost (which would be an issue for me),
- being compiler-dependent,
- only working at runtime,
- being extremely complicated.
My hope is that some better solutions emerge when these problems are not regarded separately.
Background
I want to callback Python functions in a C extension of Python in automatically generated code.
So, for example, I need foo(1,2,3)
to expand to:
PyObject_CallObject( callback_foo, Py_Build_Value("(Oddd)",Y,1,2,3) )
I know that all arguments of foo
are doubles, but I do not know their number.
(The above example is somewhat simplified. I am aware that it is missing a few Py_DECREF
s.)