UB in macros? (MS VC compiler does unbelievable tricks)

Sure it does.
Here we go:
#define Yad(x) Y ## x
Yad(()
Result - error.

Here we go again:
#define Yad(x) (
#define REDO(x) x
#define AREDO(x) #x A ## x
#define A(x) A ## REDO(x)

A(Yad(x))
Result - "(" A(
Is it conformable with C++ Standart (This trick doesnt work in gcc)
Why it works so




Answer this question

UB in macros? (MS VC compiler does unbelievable tricks)

  • noahmattern

    <noobOOP@discussions.microsoft.com> wrote in message
    news:d69843ac-76b7-4797-82c1-2c0fd203c173@discussions.microsoft.com
    > Sure it does.
    > Here we go:
    > #define Yad(x) Y ## x
    > Yad(()
    > Result - error.
     
    Not surprising. Macro invocation is missing a closing paren. A () pair is treated as part of argument x, together with the rest of the translation unit (unless you happen to have an unmatched right paren in there somewhere).

    > Here we go again:
    > #define Yad(x) (
    > #define REDO(x) x
    > #define AREDO(x) #x A ## x
    > #define A(x) A ## REDO(x)
    > A(Yad)
    > Result - "(" A(
    > Is it conformable with C++ Standart (This trick doesnt work in gcc)
    I guess you mean
     
    #define Yad (
     
    (no parameter list). According to my reading of the standard, this should have produced the same error, since at one point the preprocessor should have wound up with AREDO( ( ) that it had to rescan for more macros.
    --
    With best wishes,
        Igor Tandetnik

  • BPeck

    Could you please log the issue at http://lab.msdn.microsoft.com/productfeedback/default.aspx where the responsible folks can take a look

    Thanks in advance for taking the time to report the issue!

    Thanks,
      Ayman Shoukry
      VC++ Team

  • Bruno Denuit - MSFT

    First arent suprising for me too, but the second :)

    But this bug can be usefull, should MS programmers fix it :D

  • SujathaJaganathan

    Ok, I will do it.


    BEGGING MY PARDONS, there were a little error in the source code, so it's work wasnt correctly (bug wasnt seen). But now I fix it, so the bug is now seen.
    Submitted to lab.

  • UB in macros? (MS VC compiler does unbelievable tricks)