Consider a function
void f() {
assert(condition);
...
}
In debug mode, where asserts are enabled, the compiler is free to assume
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
/*...*/
}
A part the verb "assume", that makes the question a little obscure, (and makes me worry abot you have a real understandment of what a compiler really does) the assert
is a macro that is replaced by nothing when NDEBUG
is defined (normally in release builds), and -when used in debug builds- throws an error if condition is not true.
The point, here, is that the program is "correct" only if condition is always true. After you proved it (in testing builds) there is no point in continue to prove it even in release code.
The more subtle problem can be if "condition" itself has a side effect you don't want to remove in release builds.
In this case, just put "condition" out from the assert, store the result in a bool and assert on the bool, like
bool condition_fine = (condition);
assert(condition_fine);
This let condition to be evaluated in any case, but removes the error check from release builds.