I want to disable compiler optimization for a single function or block of code. I'm in Visual studio 2019 Environment.
I try below:
__forceinline void InsertJunkCode() {
#pragma optimize("", off)
.... Junk Code ....
#pragma optimize("", on)
}
...
int main() {
...
InsertJunkCode();
...
}
...
Error: C2156 pragma must be outside function
...
Is there a way to disable optimization for a single function or block of code?
The optimise pragma works on whole functions only, not arbitrary code segments. As such the pragma must appear outside of any function and applies to all functions defined between the on and the off.
This is clear from the documentation: