How to debug macros efficiently in VS?

前端 未结 9 1758
情书的邮戳
情书的邮戳 2021-02-01 22:38

I\'ve got a pretty complicated macro inside my (unmanaged) C++ code. Is there any way to expand macros in VS debugger? Or maybe there is another way to debug macros there?

9条回答
  •  悲哀的现实
    2021-02-01 23:06

    Use inline functions (force inlining if you really mean it) and stick to it! This will be more efficient (get only inlined if it brings performance if you optimize for perf) and maintainable. You loose much by using macros: type checking & safety.

    Macros do not exist for the compiler, they only do for the preprocessor. This one makes text replacements without looking for side effects. So macros are actually never called and you cannot debug into them. No matter how you code a macro, misuse is always possible. Like in this example:

    #define SQUARE(a) ((a)*(a))
    

    Note already the ( `)´ to protect against replacements like SQUARE(3+2). But they do not protect against:

    int a = 1;
    int b = SQUARE(++a);
    

    One would expect it to be 4 but you end up with 6 which is not really square. As it expands to int b = (++a)*(++a); You wouldn't have this trouble with inlined functions.

    Note that the implementation of an inlined function has to be visible for the compiler at the places where you use it. Also debugging will be weird to the novice user: as there are many instances of an inlined function, you'll get a huge list of methods if you want to move the execution point to another place for example.

提交回复
热议问题