Recursive C macro not expanded

前端 未结 3 881
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 12:38

I am working on a recursive macro. However, it seems that it is not expanded recursively. Here is a minimal working example to show what I mean:

// ignore input,         


        
相关标签:
3条回答
  • 2021-01-21 13:08

    The compiler pre-processor will not re expand the macro that you define. That is it will blindly replace whatever string is found in the macro statement with the string that it finds in the definition. For example, Can we have recursive macros? or Macro recursive expansion to a sequence and C preprocessor, recursive macros

    That is, recursive(a,b,c,d) will be expanded to a:recursive(b,c,d) and the pre-processor will then continue to the next line in the base code. It will not loop around to try to continue to expand the string (see the links that I cited).

    0 讨论(0)
  • 2021-01-21 13:13

    You can't get the behaviour you want. The C preprocessor is, by design, not turing complete.

    You can use multiple macros to get multiple replacements, but you will not achieve true recursion with an arbitrary number of replacements.

    0 讨论(0)
  • 2021-01-21 13:29

    As others have mentioned, pure recursion is impossible with C macros. It is, however, possible to simulate recursion-like effects.

    The Boost Pre-Processor tools do this well for both C and C++ and are a stand-alone library:

    http://www.boost.org/doc/libs/1_60_0/libs/preprocessor/doc/index.html

    0 讨论(0)
提交回复
热议问题