Optimization, asserts and release mode

前端 未结 2 561
再見小時候
再見小時候 2021-01-11 15:04

Consider a function

void f() {
   assert(condition);

   ...
}

In debug mode, where asserts are enabled, the compiler is free to assume

2条回答
  •  暖寄归人
    2021-01-11 15:29

    This can't be done in portable C or C++.

    Some compilers provide intrinsic functions such as __assume (for MSVC) and __builtin_unreachable (for GCC, ICC, and Clang), that can be used for this purpose.

    For example:

    void f() {
        __assume(condition); //For MSVC
        /*...*/
    }
    
    void f() {
        if (!condition) __builtin_unreachable(); //for GCC and Clang
        /*...*/
    }
    

提交回复
热议问题