c - 带有 _Pragma 的宏 - 括号

标签 c c-preprocessor pragma

我想通过创建适当的宏来简化一些预处理器代码。我想包装

#ifdef _OPENMP
    _Pragma("omp critical(stdout)")
#endif

成单

_OMP_CRITICAL(stdout)

我也是这样

#ifdef _OPENMP
    #define STRINGIFY(x) #x
    #define _OMP_CRITICAL(x)    _Pragma(STRINGIFY(omp critical(x)))
#else
    #define _OMP_CRITICAL(x)
#endif

它会扩展到我所期望的吗?什么将作为“参数”传递给 STRINGIFYomp critical(x value),或者omp critical(x value?这种情况下括号是否匹配?

程序运行正常。但我不太了解宏,所以它可能会产生一些编译代码,有时会工作,有时不会。并行的事情很奇怪,你可能知道,所以我想确保它是正确的。

最佳答案

是的,必须正确匹配。引用C11 (n1750) §6.10.3 ¶10 (强调我的):

A preprocessing directive of the form

# define identifier lparen identifier-listopt ) replacement-list new-line
# define identifier lparen ... ) replacement-list new-line
# define identifier lparen identifier-list , ... ) replacement-list new-line

defines a function-like macro with parameters, whose use is similar syntactically to a function call. The parameters are specified by the optional list of identifiers, whose scope extends from their declaration in the identifier list until the new-line character that terminates the #define preprocessing directive. Each subsequent instance of the function-like macro name followed by a ( as the next preprocessing token introduces the sequence of preprocessing tokens that is replaced by the replacement list in the definition (an invocation of the macro). The replaced sequence of preprocessing tokens is terminated by the matching ) preprocessing token, skipping intervening matched pairs of left and right parenthesis preprocessing tokens. Within the sequence of preprocessing tokens making up an invocation of a function-like macro, new-line is considered a normal white-space character.

关于c - 带有 _Pragma 的宏 - 括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50896969/

相关文章:

c - 在为 sg_copy_buffer 复制内存期间是否需要禁用 IRQ?

c - C 中的实现堆栈

我能否找出我包含在我的 C 程序中的库实际被使用了?

c - 一个线程控制多个其他线程

c - #ifdef 跨多个文件时出现问题

c - 生成带有前缀的宏的宏函数(避免字符串化)

c - #pragma 在 C 中不能正常工作?

c - 在 C 中使用 #define 创建数组

c - 使用预处理器将 char 数组替换为索引

SQLite:如何使用 PRAGMA application_id?