优化,断言和释放模式 [英] Optimization, asserts and release mode

查看:115
本文介绍了优化,断言和释放模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个函数

void f() {
   assert(condition);

   ...
}

在调试模式,断言启用,编译器是免费的假设条件成立,因为如果没有剩余code将不会被执行。

In debug mode, where asserts are enabled, the compiler is free to assume condition holds, since the remaining code will not be executed if it does not.

不过,在释放模式,相信编译器将只能看到

However, in release mode, I believe the compiler will only see

void f() {
   ...
}

和不能再假定条件

是否有任何编译器指令或静态断言技巧,让编译器知道某些不变量?

Are there any compiler directives or static assert tricks to let compiler know about certain invariants?

推荐答案

这不能便携式C或C ++完成。

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

有些编译器提供内在的功能,如 __assume (MSVC的)和 __ builtin_unreachable (为的 GCC ,ICC和),即可以用于此目的。

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.

例如:

void f() {
    __assume(condition); //For MSVC
    /*...*/
}

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

这篇关于优化,断言和释放模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆