How portable is code with #pragma optimize?

隐身守侯 提交于 2019-12-05 10:45:14

#pragma is the sanctioned and portable way for compilers to add non-sanctioned and non-portable language extensions *.

Basically, you never know for sure, and at least one major C++ compiler (g++) does not support this pragma as is.


*:

From the C++ standard (N3242):

16.6 Pragma directive [cpp.pragma]

A preprocessing directive of the form

# pragma pp-tokensopt new-line

causes the implementation to behave in an implementation-defined manner. The behavior might cause translation to fail or cause the translator or the resulting program to behave in a non-conforming manner. Any pragma that is not recognized by the implementation is ignored.

From the C standard (Committee Draft — April 12, 2011):

6.10.6 Pragma directive

Semantics

A preprocessing directive of the form

# pragma pp-tokensopt new-line

where the preprocessing token STDC does not immediately follow pragma in the directive (prior to any macro replacement)174) causes the implementation to behave in an implementation-defined manner. The behavior might cause translation to fail or cause the translator or the resulting program to behave in a non-conforming manner. Any such pragma that is not recognized by the implementation is ignored.

And here's an example:

int main () {
    #pragma omp parallel for
    for (int i=0; i<16; ++i) {}
}

A big part of the C and C++ OpenMP API is implemented as #pragmas.

Often this is not a good idea to rely on compiler flags, since each compiler has its own behaviour.

This flag should not be used as it is a compiling level spec you inject into your code.

Normally and theoretically this flag should be ignored by compilers if not used.

The #pragma keyword is portable in the sense that it should always compile despite on the compiler. However, the pragmas are compiler-specific so it's probable that when changing compiler it will complain with some warnings. Some pragmas are wide used, such as these from OpenMP. In order to make the code the most portable possible, you might surround your pragmas with #ifdef/#endif that depend on the compiler you're using. For example:

#ifdef __ICC
   #pragma optimize
#endif

Compilers usually define some macros such as __ICC that make the code know which compiler is being used.

Any use of #pragma is compiler specific.

For example : GNU, Intel and IBM :

#warning "Do not use ABC, which is deprecated. Use XYZ instead."

Microsoft :

#pragma message("Do not use ABC, which is deprecated. Use XYZ instead.")

Regarding your specific question about the #pragma optimize, it is supported by gcc and microsoft, but it doesn't mean it will be in the future.

Tom Tanner

#pragma is not portable, full stop. There was a version of gcc that used to start of a game whenever it came across that

Of the compilers we use at work, two definitely don't support #pragma optimise, and I can't answer for the others.

And even if they did, as the command line switches for optimisation are different, the chances are that the options for the pragma would be different.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!