gpt4 book ai didi

c - 带有 _Pragma 的宏 - 括号

转载 作者:太空宇宙 更新时间:2023-11-04 07:54:54 25 4
gpt4 key购买 nike

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

#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/

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