I copied some examples of how to implement variadic macros, and expanded on that for C basic types. It mostly worked, you'll always have difficulty for corner cases like separating pointers and arrays, but it worked well for the basic C types.
I gave up for a few reasons:
- I wanted a form to register new types, so it could work for user-defined types;
- the C pre-processor knows nothing about lists that can be expanded multiple times;
- variadic C macros are ugly hacks.
Maybe one day I'll get back to it and publish it.The interesting part is that _Generic combined with macros allows some very interesting tools for implementing primitive forms of polymorphism. Actually, if the C pre-processor supported lists, it would be possible to implement RTTI in C.
> - the C pre-processor knows nothing about lists that can be expanded multiple times;
I'm actually working on both features as Clang extensions.
#repeat, a preprocessor directive to loop, can be combined with _Pragma(push_macro/pop_macro) to create lists by redefining a macro.
and currently #increment, though I think I want to expand on this so that other macros can be redefined more easily to create lists via push/pop macro.
The reason push_macro/pop_macro pragmas can't work, is the macro has to be undefined and redefined, and the value then pushed onto a stack in the compiler.
and you can't redefine a macro in the body of another macro directly.
so I've been thinking about maybe a _Pragma(redefine_macro(MacroToRedefine, NewValueForRedefinedMacro))
but I don't want it to be limited to the _Pragma area of the compiler, I want it to be eventually standardized.
I've been talking to a friend at WG14 who suggested making it a "Preprocessor Expression, like `__has_c_attribute` and `defined()`
So that's the area I've been working on recently for the Increment/Redefine PE lately.
If you get anything close to that working, that would be a godsend. It is the last remaining piece of the puzzle for me to implement complete RTTI in C. It would certainly help to minimize glib boiler plate code too.
I'd really like it to be part of c2x, but I think it is too late now. If it is implemented by either GCC or Clang, the remaining other would certainly it too since it is too useful. So getting it to work in any of these would be good enough for me.
How can I track/follow your progress?
And if there was also __EVAL__ to force the macro preprocessor into another evaluation level, you could write recursive macros quite easily, e.g., to wrap every argument into a function call:
#define EACH(f,x,...) f(x) __VA_OPT__(, __EVAL__(EACH(f, __VA_ARGS__)))
This would make the macro magic for this library trivial: you could process lists recursively.Edit: added missing paren
Yes, I had the same urge. You can easily fall into the trap of too many features on the list. I settled on keeping user types out: you can always write a stringify() and pass that to the printf. Not the same, I know. But a more finite project.
> - the C pre-processor knows nothing about lists that can be expanded multiple times;
Yeah, that's a hack. Look at the 'VA_EXP()' macros in include/va_print/base.h. Ugly. Incomprehensible.
> - variadic C macros are ugly hacks.
Absolutely. But I think there is no other way in C.
> Actually, if the C pre-processor supported lists, it would be possible to implement RTTI in C.
I couldn't resist to put in '%t' which prints the C type of the argument...