is there any way to disable compiler optimisation for a specific line of code in Visual studio?
3
-
1What is the specific usage scenario you have? There may be other ways to achieve what you want.– clstrfsckJan 17, 2011 at 13:21
-
1Often a line of code is optimised taking in to account lots of other lines of code - so turning off optimisation for one line doesn't seem to make much sense. Is it allowed to use precomputed constant values from earlier lines, for example?– AshleysBrainJan 17, 2011 at 18:02
-
1Simple use case is to be able to add a breakpoint to that line of code, without having to run without optimizations for the rest of the code.– john16384Aug 10, 2018 at 7:44
Add a comment
|
2 Answers
No.
Only on a function-by-function basis using the optimize pragma:
#pragma optimize( "[optimization-list]", {on | off} )
The optimize pragma must appear outside a function and takes effect at the first function defined after the pragma is seen. The on and off arguments turn options specified in the optimization-list on or off.
usage:
#pragma optimize( "", off )
.
.
.
#pragma optimize( "", on )
-
1Just wanted to add that having an empty [optimization-list] is perfectly fine.– Mo0glesMar 19, 2019 at 8:37
-
I tried clang, gcc, arm, and msvc on godbolt using -O2, the only thing that seemed affected was MSVC. The description kind of makes it sound like a one time usage thing, but you can turn optimizations off before a function and then turn them back on immediately afterwards. Oct 15, 2019 at 22:06