c++ - 使用#pragma 优化的代码的可移植性如何?

标签 c++ c compiler-construction portability pragma

使用 #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-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.

来自 C 标准(委员会草案 — 2011 年 4 月 12 日):

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.

下面是一个例子:

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/

相关文章:

c++ - 在调用 QGraphicsItem::paint 之前

使用 LLVM2 编译时,iOS App 在 3.1.3 下崩溃

c++ - 如何使用类成员指针反转字符数组?

c++ - 函数 Simd::Fill() 的奇怪行为

c - 如何将 PCM 数据转换为十六进制数据

c - 24 小时时间格式正则表达式

类型转换 sizeof 中包含的结构类型

c++ - 与 gcc 相比,comeau 编译器值得吗?

compiler-construction - 你什么时候可以声称你的程序是 "compiler"?

c++ - 为什么 .o(目标文件)链接速度比 .lib(静态库)快?