带有 _Pragma 的宏 - 括号

Macro with _Pragma - parenthesis

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

#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

它会扩展到我所期望的吗?什么将作为 "argument" 传递给 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.