为什么#pragma optimize("", off)

2022-01-12 00:00:00 visual-studio c++ mfc

我正在审查一个 C++ MFC 项目.在一些文件的开头有这样一行:

I'm reviewing a C++ MFC project. At the beginning of some of the files there is this line:

#pragma optimize("", off)

我知道这会关闭所有以下功能的优化.但这样做的动机通常是什么?

I get that this turns optimization off for all following functions. But what would the motivation typically be for doing so?

推荐答案

我见过生产代码是正确的,但过于复杂以至于让优化器产生不正确的输出.这可能是关闭优化的原因.

I've seen production code which is correct but so complicated that it confuses the optimiser into producing incorrect output. This could be the reason to turn optimisations off.

但是,我认为代码更有可能只是错误,具有未定义的行为.优化器会暴露这一点并导致不正确的运行时行为或崩溃.如果没有优化,代码恰好工作".而不是找到并消除潜在问题,而是通过禁用优化并将其留在那个位置来修复"它.

However, I'd consider it much more likely that the code is simply buggy, having Undefined Behaviour. The optimiser exposes that and leads to incorrect runtime behaviour or crashes. Without optimisations, the code happens to "work." And rather than find and remove the underlying problem, someone "fixed" it by disabling optimisations and leaving it at that.

当然,这几乎是脆弱和变通办法所能得到的.新硬件、新操作系统补丁、新编译器补丁,任何这些都可能破坏这样的修复".

Of course, this is about as fragile and workarounds can get. New hardware, new OS patch, new compiler patch, any of these can break such a "fix."

即使编译指示是出于第一个原因,它也应该被大量记录.

Even if the pragma is there for the first reason, it should be heavily documented.

相关文章