How to hint to GCC that a line should be unreachable at compile time?

前端 未结 4 406
渐次进展
渐次进展 2021-01-04 03:20

It\'s common for compilers to provide a switch to warn when code is unreachable. I\'ve also seen macros for some libraries, that provide assertions for unreachable code.

4条回答
  •  有刺的猬
    2021-01-04 04:11

    With gcc 4.4.0 Windows cross compiler to PowerPC compiling with -O2 or -O3 the following works for me:

    #define unreachable asm("unreachable\n")

    The assembler fails with unknown operation if the compiler doesn't optimise it away because it has concluded that it is unreachable.

    Yes, it is quite probably `highly unpredictable under different optimization options', and likely to break when I finally update the compiler, but for the moment it's better then nothing.

提交回复
热议问题