Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

c++ - Foreach macro on macros arguments

I wonder if it is possible to write a macro foreach on macros arguments. Here is what want to do:

#define PRINT(a) printf(#a": %d", a)
#define PRINT_ALL(...) ? ? ? THE PROBLEM ? ? ? 

And possible usage:

int a = 1, b = 3, d = 0;
PRINT_ALL(a,b,d);

Here is what I achieved so far

#define FIRST_ARG(arg,...) arg
#define AFTER_FIRST_ARG(arg,...) , ##__VA_ARGS__     
#define PRINT(a) printf(#a": %d", a)
#define PRINT_ALL PRINT(FIRST_ARG(__VA_ARGS__)); PRINT_ALL(AFTER_FIRST_ARG(__VA_ARGS__))

This is a recursive macro, which is illegal. And another problem with that is stop condition of recursion.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, recursive macros are possible in C using a fancy workaround. The end goal is to create a MAP macro which works like this:

#define PRINT(a) printf(#a": %d", a)
MAP(PRINT, a, b, c) /* Apply PRINT to a, b, and c */

Basic Recursion

First, we need a technique for emitting something that looks like a macro call, but isn't yet:

#define MAP_OUT

Imagine we have the following macros:

#define A(x) x B MAP_OUT (x)
#define B(x) x A MAP_OUT (x)

Evaluating the macro A (blah) produces the output text:

blah B (blah)

The preprocessor doesn't see any recursion, since the B (blah) call is just plain text at this point, and B isn't even the name of the current macro. Feeding this text back into the preprocessor expands the call, producing the output:

blah blah A (blah)

Evaluating the output a third time expands the A (blah) macro, carrying the recursion full-circle. The recursion continues as long as the caller continues to feed the output text back into the preprocessor.

To perform these repeated evaluations, the following EVAL macro passes its arguments down a tree of macro calls:

#define EVAL0(...) __VA_ARGS__
#define EVAL1(...) EVAL0 (EVAL0 (EVAL0 (__VA_ARGS__)))
#define EVAL2(...) EVAL1 (EVAL1 (EVAL1 (__VA_ARGS__)))
#define EVAL3(...) EVAL2 (EVAL2 (EVAL2 (__VA_ARGS__)))
#define EVAL4(...) EVAL3 (EVAL3 (EVAL3 (__VA_ARGS__)))
#define EVAL(...)  EVAL4 (EVAL4 (EVAL4 (__VA_ARGS__)))

Each level multiplies the effort of the level before, evaluating the input 365 times in total. In other words, calling EVAL (A (blah)) would produce 365 copies of the word blah, followed by a final un-evaluated B (blah). This provides the basic framework for recursion, at least within a certain stack depth.

End Detection

The next challenge is to stop the recursion when it reaches the end of the list.

The basic idea is to emit the following macro name instead of the normal recursive macro when the time comes to quit:

#define MAP_END(...)

Evaluating this macro does nothing, which ends the recursion.

To actually select between the two macros, the following MAP_NEXT macro compares a single list item against the special end-of-list marker (). The macro returns MAP_END if the item matches, or the next parameter if the item is anything else:

#define MAP_GET_END() 0, MAP_END
#define MAP_NEXT0(item, next, ...) next MAP_OUT
#define MAP_NEXT1(item, next) MAP_NEXT0 (item, next, 0)
#define MAP_NEXT(item, next)  MAP_NEXT1 (MAP_GET_END item, next)

This macro works by placing the item next to the MAP_GET_END macro. If doing that forms a macro call, everything moves over by a slot in the MAP_NEXT0 parameter list, changing the output. The MAP_OUT trick prevents the preprocessor from evaluating the final result.

Putting it All Together

With these pieces in place, it is now possible to implement useful versions of the A and B macros from the example above:

#define MAP0(f, x, peek, ...) f(x) MAP_NEXT (peek, MAP1) (f, peek, __VA_ARGS__)
#define MAP1(f, x, peek, ...) f(x) MAP_NEXT (peek, MAP0) (f, peek, __VA_ARGS__)

These macros apply the operation f to the current list item x. They then examine the next list item, peek, to see if they should continue or not.

The final step is to tie everything together in a top-level MAP macro:

#define MAP(f, ...) EVAL (MAP1 (f, __VA_ARGS__, (), 0))

This macro places a () marker on the end of the list, as well as an extra 0 for ANSI compliance (otherwise, the last iteration would have an illegal 0-length list). It then passes the whole thing through EVAL and returns the result.

I have uploaded this code as a library on github for your convenience.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...