在宏中访问__va_args__的内容(不是函数)

发布于 2025-02-05 00:34:07 字数 527 浏览 2 评论 0原文

一个人可以使用...的内容使用stdarg.h

void fn(int nargs, ...){
  va_list args; va_start(args,nargs);
  i64 arg0 = va_arg(args,i64);
  va_end(args);
}

我知道使用__ va_args __的唯一方法是通过它到另一个宏或最终是一个函数。例如。

#define __fn(...)  fn(number_of_args(__VA_ARGS__), __VA_ARGS__)

但是我想知道是否可以在宏本身中“解开” __ va_args __的值。类似va_start()va_arg()va_end(),但对于宏。

One can access the contents of ... inside a function using stdarg.h:

void fn(int nargs, ...){
  va_list args; va_start(args,nargs);
  i64 arg0 = va_arg(args,i64);
  va_end(args);
}

The only way I know of using __VA_ARGS__ is to pass it to another macro or, eventually, a function. Eg.

#define __fn(...)  fn(number_of_args(__VA_ARGS__), __VA_ARGS__)

but I wonder if it's possible to "unpack" the values of __VA_ARGS__ within a macro itself. Something like va_start(), va_arg(), and va_end(), but for macros.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁客 2025-02-12 00:34:07

如果可以在宏本身中“解开” va_args 的值。

是的,您可以获取#define head(x,...)x的x ,并在列表上迭代eval(evar(...递归扩展) 。 pfultz2/cloak/wiki/c-preprocessor-tricks,-tips, - 和idioms

。 扩展,因此我更喜欢为每个参数明确过载。

if it's possible to "unpack" the values of VA_ARGS within a macro itself.

Yes, you can get the head #define HEAD(x, ...) x of the pack and iterate over the list with EVAL(EVAL(... recursive expansion. See https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms .

I find overloading on the number of macro arguments to produce way more readable error messages than EVAL(EVAL(.. expansions, so I prefer explicitly overloading for each argument.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文