c++optimization

Not compiler optimizing portions of code in C++


Is there a way to tell the compiler to not optimize selective portions of code? I know you can tell the compiler to not optimize away certain variables using volatile but what about entire chunks of code?


Solution

  • In most cases, yes, but the exact method depends on your particular compiler.

    Typically it will be something like:

    #pragma optimize(off)
    

    Anything optimizer-related is completely outside the scope of the standard. The only part the standard plays is in mandating the behavior, but the compiler can do any optimizations that don't violate the required behavior, or none at all.

    Documentation for g++:

    Documentation for MS Visual C++ (select version once you get there):