Why #pragma optimize(“”, off)

前端 未结 4 1252
走了就别回头了
走了就别回头了 2021-02-04 03:16

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 o

4条回答
  •  臣服心动
    2021-02-04 03:46

    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.

提交回复
热议问题