Would a modern compiler (GCC) be smart enough to optimize this no-op function out? I know that they can optimize no-op's out but I'm wondering how well they can determine if a function call is actually a no-op.
void someFunction(){
if false{
doFunction()
}
}
Well, for a start, it wouldn't compile that at all since you're missing parentheses around your condition :-)
But, with that fixed, I think you'll find that the if
statement itself (and it's body) can be optimised out of existence. The someFunction()
function will almost certainly remain since you may call it from somewhere, and that somewhere may be from a totally different translation unit (source file).
If it was static and the compiler could deduce that the function was not used, it would be possible to remove the whole function. That works because, being static, it would not be callable from elsewhere.
However, you'd have to be careful. Calling functions is not the only way to use them, there's other possibilities such as taking the address of them and using that, such as with callbacks.