Re: When may an [optimizing] C++ compiler omit object instantiations?
Juha Nieminen wrote:
John Lampe wrote:
What's preventing the compiler from optimizing the entire construction of
'_mutex' out?
The constructor and the destructor of mutex_taker probably have
side_effects (iow. they modify the parameter g_mutex, which is not
local). Optimizing something away can only be done if doing that does
not change the behavior of the program. A function call with
side-effects thus cannot be optimized away because doing so would change
the behavior.
Not to rain on your parade, but [class.copy/15] states that "When
certain criteria are met, an implementation is allowed to omit the copy
construction of a class object, even if the copy constructor and/or
destructor for the object have side effects." -- the important part here
is after the second comma. So, side effects or not, some functions are
allowed to be optimized away "when certain criteria are met".
If the compiler can prove (to itself) that a function call has no
side-effects and its behavior is akin to a no-op in the context, then it
might optimize the call away. If it cannot prove it (eg. because it
doesn't see the implementation at that point) then it cannot optimize it
away.
The proof is not necessary. If the Standard allows it, that's all that
is needed.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask