Simple ways to disable parts of code

后端 未结 14 1137
误落风尘
误落风尘 2021-01-31 08:59

This isn\'t a typical question to solve a specific problem, it\'s rather a brain exercise, but I wonder if anyone has a solution.

In development we often need to disable

14条回答
  •  借酒劲吻你
    2021-01-31 09:20

    If you're doing checks at compile time, you can use Gigi's answer, which will conditionally not compile sections of code. Note that the preprocessor has no knowledge of variables, or sizeof, or other things handled by the compiler (so using something like 4 == sizeof(int) will not fly)

    If you want to compile in little bits of debugging code that should not ever get run, you can use regular conditional statements, like such

    bool debugging = false;
    
    // banana banana banana
    
    if (debugging)
    {
        // do a bunch of stuff here
    }
    

    You can then use a debugger to access the skipped section by assigning debugging to true.

提交回复
热议问题