可变参数宏技巧

发布于 2024-10-24 09:38:42 字数 316 浏览 1 评论 0原文

创建可变参数宏 FOO(a1, a2, a3,..., an) 使其扩展为 FOOn(a1, a2, a3,..., an) 的技巧是什么) 对于 n 的值在您选择的任何预选有界范围内?也就是说,FOO(a) 应扩展为 FOO1(a)FOO(a, b, c) 扩展为 FOO3( a、b、c) 等。我知道有一个标准技巧,但我似乎找不到它。

如果有其他问题的答案,请随时将此问题标记为重复并关闭它。我怀疑有,但我找不到。

What's the trick to create a variadic macro FOO(a1, a2, a3,..., an) such that it expands to FOOn(a1, a2, a3,..., an) for values of n in whatever preselected bounded range you choose? That is, FOO(a) should expand to FOO1(a), FOO(a, b, c) to FOO3(a, b, c), etc. I know there's a standard trick but I can't seem to find it.

Please feel free to mark this question as a duplicate and close it if there's another question with the answer. I suspect there is but I couldn't find it.

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

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

发布评论

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

评论(3

故事与诗 2024-10-31 09:38:42
#define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
#define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)

#define FOO_IMPL2(count, ...) FOO ## count (__VA_ARGS__)
#define FOO_IMPL(count, ...) FOO_IMPL2(count, __VA_ARGS__) 
#define FOO(...) FOO_IMPL(VA_NARGS(__VA_ARGS__), __VA_ARGS__)

FOO(a)
FOO(a, b)
FOO(a, b, c)

调用被替换为:

FOO1 (a)
FOO2 (a, b)
FOO3 (a, b, c)
#define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
#define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 5, 4, 3, 2, 1)

#define FOO_IMPL2(count, ...) FOO ## count (__VA_ARGS__)
#define FOO_IMPL(count, ...) FOO_IMPL2(count, __VA_ARGS__) 
#define FOO(...) FOO_IMPL(VA_NARGS(__VA_ARGS__), __VA_ARGS__)

FOO(a)
FOO(a, b)
FOO(a, b, c)

The invocations are replaced by:

FOO1 (a)
FOO2 (a, b)
FOO3 (a, b, c)
甜警司 2024-10-31 09:38:42

改进詹姆​​斯的答案以增加一些灵活性:

#define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
#define VA_NARGS(...) VA_NARGS_IMPL(X,##__VA_ARGS__, 4, 3, 2, 1, 0)
#define VARARG_IMPL2(base, count, ...) base##count(__VA_ARGS__)
#define VARARG_IMPL(base, count, ...) VARARG_IMPL2(base, count, __VA_ARGS__) 
#define VARARG(base, ...) VARARG_IMPL(base, VA_NARGS(__VA_ARGS__), __VA_ARGS__)

#define MyMacro0() Also works without arguments.
#define MyMacro2(x,y) [x...y]
#define MyMacro(...) VARARG(MyMacro, __VA_ARGS__)

MyMacro()
MyMacro(a)
MyMacro(a, b)
MyMacro(a, b, c)

输出:

Also works without arguments.
MyMacro1(a)
[a...b]
MyMacro3(a, b, c)

Improving upon James answer to add some flexibility:

#define VA_NARGS_IMPL(_1, _2, _3, _4, _5, N, ...) N
#define VA_NARGS(...) VA_NARGS_IMPL(X,##__VA_ARGS__, 4, 3, 2, 1, 0)
#define VARARG_IMPL2(base, count, ...) base##count(__VA_ARGS__)
#define VARARG_IMPL(base, count, ...) VARARG_IMPL2(base, count, __VA_ARGS__) 
#define VARARG(base, ...) VARARG_IMPL(base, VA_NARGS(__VA_ARGS__), __VA_ARGS__)

#define MyMacro0() Also works without arguments.
#define MyMacro2(x,y) [x...y]
#define MyMacro(...) VARARG(MyMacro, __VA_ARGS__)

MyMacro()
MyMacro(a)
MyMacro(a, b)
MyMacro(a, b, c)

Output:

Also works without arguments.
MyMacro1(a)
[a...b]
MyMacro3(a, b, c)
橙幽之幻 2024-10-31 09:38:42

这篇文章可变参数宏来计数我相信,论点有你正在寻找的东西。看看第一条和第二条回复。

This post Variadic macro to count number of arguments has what you're looking for I believe. Look at the first and second responses.

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