I am wondering if it can be guaranteed (by the compiler's implementation) that certain 'if' statements are never going to be implemented in binary code, i.e., no jumps for some given 'if' statements are ever going to be used in the binary code.
This can be motivated with examples, but let me be brief. Assume you have a class like this one:
template<bool N = true>
class A {
public:
// ...
void f() {
// do some work
if (N) {
// do some optional work
}
// do more work
}
// ...
};
Is the 'if' statement inside the function f ever going to be implemented by a modern compiler when class A is instantiated as A<false>? By "implemented" I mean if branching (or jump) instructions are going to be produced.
It seems to me that the answer is negative, i.e., the compiler will remove it (probably because it does not make much sense to implement if (false) { .. }). But is this guaranteed to happen in all compilers? How do I know if a compiler does this kind of optimization? Is that 'if' statement going to be removed only when optimization flags (e.g., -O1 or higher in g++) are passed to the compiler? In other words, is it also removed when there is no optimization at all?
Aucun commentaire:
Enregistrer un commentaire