为什么 gcc 会立即销毁我的对象,尽管它的范围很大? (我该如何让它不这样做?)
我编写了一个带有附带保护类的小型分析类,如下所示:
{
ProfileGuard pg("SampleName");
// Code to profile
...
}
但后来我注意到有时 gcc 会优化代码,以便立即破坏 pg
。显然它检测到 pg 和其他代码不会互相影响。阻止 gcc 这样做的最佳方法是什么?
更新: 由于对我的问题有一些严重的怀疑,我将再次检查我的代码。我的问题基于:
3.7.3 3。
如果具有自动存储期限的变量具有初始化或具有副作用的析构函数,则不应 在其块结束之前被销毁,也不应作为优化而被消除,即使它看起来 不被使用,除非类对象或其复制/移动可以按照 12.8 中的规定被消除。
打印有“副作用”吗?
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 eliminated 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"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
既然这显然是不真实的,那又如何呢?它们确实相互影响(例如通过输出顺序)。
海湾合作委员会不这样做。也许您不小心使用了匿名(因此是临时的)对象?
现在,这个将在执行表达式后立即被破坏。
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?
Now, this will be destructed immediately after executing the expression.