gpt4 book ai didi

c - c中的##是什么?

转载 作者:太空狗 更新时间:2023-10-29 16:53:48 25 4
gpt4 key购买 nike

我看过这个片段:

#define kthread_create(threadfn, data, namefmt, arg...) \
kthread_create_on_node(threadfn, data, -1, namefmt, ##arg)
  1. ## 代表什么?
  2. ## 出现在宏外时是什么意思?

最佳答案

与其他答案相反,这实际上是 GCC 扩展。直接粘贴变量参数时,如果没有传递额外的参数,就会出现问题。因此,当与 __VA_ARGS__ 或可变参数变量(用 argname... 声明)一起使用时,GCC 生成 ##。如果它包含一个值,则粘贴,如果不包含,则删除前面的逗号。

此扩展的文档是 here :

Second, the '##' token paste operator has a special meaning when placed between a comma and a variable argument. If you write

#define eprintf(format, ...) fprintf (stderr, format, ##__VA_ARGS__)

and the variable argument is left out when the eprintf macro is used, then the comma before the '##' will be deleted. This does not happen if you pass an empty argument, nor does it happen if the token preceding '##' is anything other than a comma.

eprintf ("success!\n")
==> fprintf(stderr, "success!\n");

The above explanation is ambiguous about the case where the only macro parameter is a variable arguments parameter, as it is meaningless to try to distinguish whether no argument at all is an empty argument or a missing argument. In this case the C99 standard is clear that the comma must remain, however the existing GCC extension used to swallow the comma. So CPP retains the comma when conforming to a specific C standard, and drops it otherwise.

关于c - c中的##是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12053275/

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