Search code examples
cc-preprocessorcurryingpartial-application

Is partial macro application / currying possible in the C preprocessor?


As an example of the problem, is there any way to implement the macro partialconcat in the following code?

#define apply(f, x) f(x)

apply(partialconcat(he),llo) //should produce hello

EDIT:

Here's another example, given a FOR_EACH variadic macro (see an example implementation in this answer to another question).

Say I want to call a member on several objects, probably within another macro for a greater purpose. I would like a macro callMember that behaves like this:

FOR_EACH(callMember(someMemberFunction), a, b, c);

produces

a.someMemberFunction(); b.someMemberFunction(); c.someMemberFunction();

This needs callMember(someMember) to produce a macro that behaves like

#define callMember_someMember(o) o.someMember()

Solution

  • The C preprocessor is 'only' a simple text processor. In particular, one macro cannot define another macro; you cannot create a #define out of the expansion of a macro.

    I think that means that the last two lines of your question:

    This needs callMember(someMember) to produce a macro that behaves like

    #define callMember_someMember(o) o.someMember()

    are not achievable with a single application of the C preprocessor (and, in the general case, you'd need to apply the preprocessor an arbitrary number of times, depending on how the macros are defined).