It is my understanding that modern c++ compilers take shortcuts on things like:
if(true)
{do stuff}
But how about something like:
bool foo(){return true}
...
if(foo())
{do stuff}
Or:
class Functor
{
public:
bool operator() () { return true;}
}
...
Functor f;
if(f()){do stuff}
It depends if the compiler can see foo()
in the same compilation unit.
With optimization enabled, if foo()
is in the same compilation unit as the callers, it will probably inline the call to foo()
and then optimization is simplified to the same if (true)
check as before.
If you move foo()
to a separate compilation unit, the inlining can no longer happen, so most compilers will no longer be able to optimize this code. (Link-time optimization can optimize across compilation units, but it's a lot less common--not all compilers support it and in general it's less effective.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With