c define multiline macro?

前端 未结 6 1415
滥情空心
滥情空心 2021-02-05 18:32
#define DEBUG_BREAK(a)\\
    if ((a)) \\
{\\
    __asm int 3;\\
}

I have defined a macro as above, and try to use it

#include \"test_d         


        
相关标签:
6条回答
  • 2021-02-05 19:12

    The macro

    #define DEBUG_BREAK(a)\
        if ((a)) \
        __asm int 3;
    

    works fine but

    #define DEBUG_BREAK(a)\
        if ((a)) \
    {\
        __asm int 3;\
    }
    

    doesn't! And I think anyone could guess why!! The new line operator is the problem making guy!

    It takes

     __asm int 3;\
    }
    

    as

    __asm int 3; }
    

    where ; comments out what follows it (in assembly). So we will miss a } then.

    0 讨论(0)
  • 2021-02-05 19:13
    #define DEBUG_BREAK(a)\
    if ((a)) \
    {\
        __asm \
        { \
            int 3;\
        } \
    }
    

    Or.... (since you are on Windows, just use the DebugBreak function...)

    #define DEBUG_BREAK(a) {if ((a)) DebugBreak();}
    
    0 讨论(0)
  • 2021-02-05 19:13

    That's weird, but getting {int 3} into brackets helps. And combining macro into singleliner doesn't. So it should be something about assembly, not the multilining.

    0 讨论(0)
  • 2021-02-05 19:17

    Please try this

    #define DEBUG_BREAK(a)\
        if ((a)) \
        __asm int 3;
    
    0 讨论(0)
  • 2021-02-05 19:25

    Rewrite it as an inline function:

    inline void DEBUG_BREAK(bool b)
    {
        if (b) 
        {
            __asm int 3
        }
    }
    

    You may want to replace __asm int 3 with DebugBreak(), as that is the official MS function to do this.

    0 讨论(0)
  • 2021-02-05 19:32

    Check there is no white space after each backslash. I often fall for this.

    You might even need a single space before the backslash.

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