使用 #pragma optimize
的代码的可移植性如何?大多数编译器都支持它吗?对此 #pragma
的支持有多完整?
最佳答案
#pragma
是编译器添加非认可和不可移植语言扩展的认可和可移植方式*.
基本上,您永远无法确定,并且至少有一个主要的 C++ 编译器 (g++) 不按原样支持此 pragma。
*:
来自 C++ 标准 (N3242):
16.6 Pragma directive [cpp.pragma]
A preprocessing directive of the form
# pragma
pp-tokensopt new-linecauses 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.
来自 C 标准(委员会草案 — 2011 年 4 月 12 日):
6.10.6 Pragma directive
Semantics
A preprocessing directive of the form
# pragma
pp-tokensopt new-linewhere the preprocessing token
STDC
does not immediately followpragma
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 suchpragma
that is not recognized by the implementation is ignored.
下面是一个例子:
int main () {
#pragma omp parallel for
for (int i=0; i<16; ++i) {}
}
C 和 C++ OpenMP API 的很大一部分是作为 #pragma
实现的。
关于c++ - 使用#pragma 优化的代码的可移植性如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13267648/