开发者

Why is gcc destroying my object instantly despite its scope? (and how do I make it not do that?)

I wrote a small profiling class with an accompanying guard class to use like this:

{
    ProfileGuard pg("SampleName");

    // Code to profile
    ...
}

But then I noticed that sometimes gcc optimizes the code so that pg is destructed instantly. Apparently it detects that pg and the other code will not influence eachother. What's the best way to prevent gcc from doing this?

Update: Since there is some serious disbelief concerning my question I will check my code again. I based my question on:

3.7.3 3.

If a variable with automatic storage duration has initialization or a destructor with side effects, it shall not be destroyed before the end of its block, nor shall it be elim开发者_开发问答inated as an optimization even if it appears to be unused, except that a class object or its copy/move may be eliminated as specified in 12.8.

Is doing a print a "side effect"?


Apparently it detects that pg and the other code will not influence eachother.

How, since that’s patently untrue? They do influence each other (e.g. through output order).

GCC doesn’t do this. Perhaps you accidentally used an anonymous (and hence temporary) object?

ProfileGuard("SampleName");

Now, this will be destructed immediately after executing the expression.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜