how to explain this

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zhangyefei.yefei@gmail.com

    how to explain this

    #define cat(x,y) x##y concatenates x to y. But cat(cat(1,2),3) does
    not expand but gives preprocessor warning. Why?

    thanks .
  • WANG CONG

    #2
    Re: how to explain this

    zhangyefei.yefe i@gmail.com wrote:

    <snip>
    but i have another problem :
    >But if you have...
    >>
    > #define xcat(x,y) x ## y
    > #define cat(x,y) xcat(x,y)
    >>
    >...then...
    >>
    > cat(cat(1,2),3)
    >>
    >...proceeds as...
    >>
    > xcat(cat(1,2),3 )
    >>
    >...which is scanned for replacement. Note there's no ##
    >in sight. The inner cat becomes...
    >>
    > xcat(xcat(1,2), 3)
    >
    why not xcat(1,2)##3 ?
    Because the standard defines this.

    Look at C99 6.10.3.1, it is said that:

    "A parameter in the replacement list, unless preceded by a # or ##
    preprocessing token or followed by a ## preprocessing token (see below), is
    replaced by the corresponding argument after all macros contained therein
    have been expanded."

    So we can safely conclude that if arguments of a macro contains macros,
    then the macros in arguments will be expanded first.
    >
    further more, macro replacement is how to implementation in c?
    where i can find the source program.
    thank you very much.
    I think you can read the cpp source code in gcc, I guess it's right
    in gcc/cppmacro.c .

    Have fun!

    --
    Hi, I'm a .signature virus, please copy/paste me to help me spread
    all over the world.

    Comment

    Working...