Will call to function which calls assert will be removed in Release ver of a program?
If I have something like:
static long double calcFactor_(const short mantissa, const short exponent,const short base = Derived::internals_.base_)
{
assert(mantissa > 0);
assert(mantissa < Numer开发者_如何学PythonicLimits<short>::max);
assert(exponent < NumericLimits<short>::max);
assert(exponent > NumericLimits<short>::min);
assert(base < NumericLimits<short>::max);
assert(base > NumericLimits<short>::min);
return mantissa * ::pow(static_cast<long double>(base),exponent);
}
and in other places in my program I'm also using those identical calls to assert, so I would like to move it out of this fnc body to the separate fnc and place only call to this fnc in places where I have those calls to assets now. But correct me if I'm wrong: Those calls to assert will be removed in release ver but if I have:
void Assert (//neccesary args here)
{
assert(mantissa > 0);
assert(mantissa < NumericLimits<short>::max);
assert(exponent < NumericLimits<short>::max);
assert(exponent > NumericLimits<short>::min);
assert(base < NumericLimits<short>::max);
assert(base > NumericLimits<short>::min);
}
will call to this fnc be also removed from release ver or not? And another Q I think that instead of asserts here I should have if(!condition) checks because what is the benefit of having asserts if they will be removed in a final ver. What do you think?
Yes, assert
won't make it to the release build but the calls to your Assert
function will remain. At best, your compiler might detect an empty function and suppress the call, but I wouldn't count on it. If you really want to remove these calls from the release build, you could surround them with some #ifdef
/ #endif
.
Regarding the benefits of assert, that's clearly "subjective and argumentative" so I'll pass !
You should always couple error checking with asserts. Otherwise you gain nothing in release mode since all your stability code will be gone. So if I were you, I'd put in if statements to guard against bad or invalid data comming into your function.
As for whether your 'empty' function will be removed by the compiler or not in release builds, I guess that depends on the compiler. But I would not leave the asserts as the only mechanism to determining if the input data is valid or not. I'd stick in if statements to guard against it. Therefore it kind of becomes a moot point.
I guess it depends if your compiler optimises away an empty function call (as all those assert
s will have disappeared).
精彩评论