gpt4 book ai didi

c++ - 为什么 '#pragma omp critical' 部分不能在同一行上有左大括号?

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:21:06 25 4
gpt4 key购买 nike

我经常遇到编译器错误,我忘记将 #pragma omp critical 部分的左大括号放在语句后面的行上,而不是放在同一行上:

  #pragma omp parallel 
{

static int i = 0;

// this code fails to compile
#pragma omp critical {
i++;
}

// this code compiles fine
#pragma omp critical
{
i++;
}

}

我的问题是,为什么编译器不能解析同一行的大括号?它可以为任何 C++ 语法执行此操作。为什么空白对于 OpenMP #pragma 语句很重要,而在 C++ 中却没有?

最佳答案

根据cppreference:

The preprocessing directives control the behavior of the preprocessor. Each directive occupies one line and has the following format:

  1. # character

  2. preprocessing instruction (one of define, undef, include, if, ifdef, ifndef, else, elif, endif, line, error, pragma)

  3. arguments (depends on the instruction)

  4. line break.

The null directive (# followed by a line break) is allowed and has no effect.

所以预处理器,而不是编译器,将整行作为指令读取。与不关心换行符的编译器不同。

来源:http://en.cppreference.com/w/cpp/preprocessor

关于c++ - 为什么 '#pragma omp critical' 部分不能在同一行上有左大括号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49309000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com