Why macro is said to be recursive




















I am not sure whether we can have a recursive macros or not. Your argument is flawed. The macro replacement algorithm is repeated if your macros contains other macros until no more substitution is done. So potentially it could do recursive macros. BUT the language spec explicitly forbids this by saying that once a macro is replaced it is removed from the potential list for subsequent replacement within the same line. Thanks, fixed, didn't know about that rule.

I got my first doubt clear that You cant have recursive macros. What about the error in my sample code in the question??? You don't say what error you get, but the pr used recursively in the pr macro won't get expanded, and probably results in an "undefined function" error or some such. Sign up or log in Sign up using Google.

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta.

Now live: A fully responsive profile. Linked 1. See more linked questions. Related Of course the real macro expander will walk the whole form and "force" all these promises unexpanded macro calls until no more remain. I think there are some advantages to this style, such as easy debugging with macroexpand.

You get a small expansion. If its recursive nature is obvious, that's a win. If the macro is such that your brain needs to see the whole thing expanded, it's a loss. If 1 is true, evaluate the forms list 2. Otherwise keep going with the other cond pairs.

In most cases, it is a bad idea to take advantage of this feature. A person reading the program who sees that foo is a variable will not expect that it is a macro as well. The reader will come across the identifier foo in the program and think its value should be that of the variable foo , whereas in fact the value is four greater. But I digress. To prevent recursion, cpp associates a bit with every macro that has been defined.

Cpp furthermore associates a bit with each token in the input stream, signifying that the token can never be macro-expanded. Initially, the replacing and unavailable bits are all clear. If T is the name of a macro for which the replacing bit is true, cpp sets the unavailable bit on token T. Moreover, once the unavailable has been set on an input token, it is never be cleared. Note, however, that if T is called with an invalid number of arguments, then the program is ill-formed.

If cpp decides not to macro-expand T , it simply adds T to the current output token list. Otherwise, it expands T in two phases. When T is a function-like macro, cpp scans all of the arguments supplied to T and performs macro expansion on them. It also remembers the original, non-macro-expanded arguments for use with and. It also performs stringification and pasting as indicated by and in the substitution list.



0コメント

  • 1000 / 1000