<p>I'm using a logging module that can have reporting enabled/disabled at runtime. Calls generally go something like:</p>

<pre><code>WARN(
     "Danger Will Robinson! There are "
     + boost::lexical_cast&lt;string&gt;(minutes)
     + " minutes of oxygen left!"
);
</code></pre>

<p>I'm using an inline function for WARN, but I'm curious as to how much optimization is going on behind the scenes -- evaluation of the arguments throughout the entire program would be costly. The <code>WARN</code> function goes something like this:</p>

<pre><code>bool WARNINGS_ENABLED = false;
inline void WARN(const string &amp;message) {
    if (!WARNINGS_ENABLED) {
       return;
    }
    // ...
}
</code></pre>

<p>Given that constructing the string argument has no side-effects, will the compiler optimize it out? Is a certain level of optimization required (<code>-Ox</code> in <code>g++</code> for some <code>x</code>)?</p>

I'd guess that it only has a chance to optimize it out if it can prove that there are no side effects (which might be difficult for the compiler to do for an expensive function call).
I'm not a boost expert, but I'm guessing there's a way to construct a lambda that will only be evaluated to generate the string if WARNINGS_ENABLED is true.  Something like...
```
```
inline void warnFunc(some_boost_lambda &message_generator) {
  if (WARNINGS_ENABLED) {
    cerr << message_generator() << endl;
  }
}

#define WARN(msg) warnFunc(...insert boost magic here to turn msg into a lambda...)
```
```