问题
Consider a function
void f() {
assert(condition);
...
}
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() {
...
}
and can no longer assume condition
.
Are there any compiler directives or static assert tricks to let compiler know about certain invariants?
回答1:
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
/*...*/
}
回答2:
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.
来源:https://stackoverflow.com/questions/23709259/optimization-asserts-and-release-mode