C++: avoid optimizing out variable -
i have useful code in constructor or class valuable
. want sure it's executed before submain
. how can guarantee it's not optimized out?
int main() { // dear compiler, please don't optimize ctor call out! valuable var; return submain(); }
is local variable enough? need use static
:
static valuable *v = new valuable(); delete v; v = null;
can shorten previous 1 liner:
delete new valuable();
if constructor or destructor has observable behavior, compiler not allowed optimize out. there's no need tricky.
Comments
Post a Comment