gpt4 book ai didi

c++ - 为什么我的宏参数之一被替换为 ')' 而不是标识符?

转载 作者:行者123 更新时间:2023-11-30 16:11:13 25 4
gpt4 key购买 nike

我正在为 C++ 创建一个新的模式匹配库,因此我必须进行一些高级宏元编程。我被以下错误困扰了半天,我已经没有选择了。不过,我真的很希望它能发挥作用。有人可以看一下吗?

../../test/macros.cpp:57:16: error: pasting formed ')0', an invalid preprocessing token
ZEN_FOR_EACH(DECLARE_VARS_NUMBERED, foo, bar, baz);
^
../../test/macros.cpp:57:3: error: expected ';' at end of declaration
ZEN_FOR_EACH(DECLARE_VARS_NUMBERED, foo, bar, baz);
^
../../include/zen/macros.h:167:30: note: expanded from macro 'ZEN_FOR_EACH'
#define ZEN_FOR_EACH(m, ...) ZEN_FOR_EACH_WITH(, m, __VA_ARGS__)
^
../../include/zen/macros.h:166:38: note: expanded from macro 'ZEN_FOR_EACH_WITH'
#define ZEN_FOR_EACH_WITH(s, m, ...) ZEN_REPEAT_WITH(s, ZEN_EXPAND(ZEN_VA_LENGTH(__VA_ARGS__)), ZEN_FOR_EACH_IMPL, m, __VA_ARGS__)
^
../../include/zen/macros.h:117:36: note: expanded from macro 'ZEN_REPEAT_WITH'
#define ZEN_REPEAT_WITH(s,n,m,...) ZEN_CONCAT(ZEN_REPEAT_WITH_, n)(s,m,__VA_ARGS__)
^
note: (skipping 1 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
../../include/zen/macros.h:67:31: note: expanded from macro 'ZEN_CONCAT_IMPL'
#define ZEN_CONCAT_IMPL(a, b) a ## b
^
<scratch space>:3:1: note: expanded from here
ZEN_REPEAT_WITH_3
^
../../include/zen/macros.h:100:38: note: expanded from macro 'ZEN_REPEAT_WITH_3'
#define ZEN_REPEAT_WITH_3(s,m,...) m(0,__VA_ARGS__) s m(1,__VA_ARGS__) s m(2,__VA_ARGS__)
^
../../test/macros.cpp:57:16: error: pasting formed ')1', an invalid preprocessing token
ZEN_FOR_EACH(DECLARE_VARS_NUMBERED, foo, bar, baz);
^
../../test/macros.cpp:57:3: error: expected ';' at end of declaration
ZEN_FOR_EACH(DECLARE_VARS_NUMBERED, foo, bar, baz);
^
../../include/zen/macros.h:167:30: note: expanded from macro 'ZEN_FOR_EACH'
#define ZEN_FOR_EACH(m, ...) ZEN_FOR_EACH_WITH(, m, __VA_ARGS__)
^
../../include/zen/macros.h:166:38: note: expanded from macro 'ZEN_FOR_EACH_WITH'
#define ZEN_FOR_EACH_WITH(s, m, ...) ZEN_REPEAT_WITH(s, ZEN_EXPAND(ZEN_VA_LENGTH(__VA_ARGS__)), ZEN_FOR_EACH_IMPL, m, __VA_ARGS__)
^
../../include/zen/macros.h:117:36: note: expanded from macro 'ZEN_REPEAT_WITH'
#define ZEN_REPEAT_WITH(s,n,m,...) ZEN_CONCAT(ZEN_REPEAT_WITH_, n)(s,m,__VA_ARGS__)
^
note: (skipping 1 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
../../include/zen/macros.h:67:31: note: expanded from macro 'ZEN_CONCAT_IMPL'
#define ZEN_CONCAT_IMPL(a, b) a ## b
^
<scratch space>:3:1: note: expanded from here
ZEN_REPEAT_WITH_3
^
../../include/zen/macros.h:100:57: note: expanded from macro 'ZEN_REPEAT_WITH_3'
#define ZEN_REPEAT_WITH_3(s,m,...) m(0,__VA_ARGS__) s m(1,__VA_ARGS__) s m(2,__VA_ARGS__)
^
../../test/macros.cpp:57:16: error: pasting formed ')2', an invalid preprocessing token
ZEN_FOR_EACH(DECLARE_VARS_NUMBERED, foo, bar, baz);
^
../../test/macros.cpp:57:3: error: expected ';' at end of declaration
ZEN_FOR_EACH(DECLARE_VARS_NUMBERED, foo, bar, baz);
^
../../include/zen/macros.h:167:30: note: expanded from macro 'ZEN_FOR_EACH'
#define ZEN_FOR_EACH(m, ...) ZEN_FOR_EACH_WITH(, m, __VA_ARGS__)
^
../../include/zen/macros.h:166:38: note: expanded from macro 'ZEN_FOR_EACH_WITH'
#define ZEN_FOR_EACH_WITH(s, m, ...) ZEN_REPEAT_WITH(s, ZEN_EXPAND(ZEN_VA_LENGTH(__VA_ARGS__)), ZEN_FOR_EACH_IMPL, m, __VA_ARGS__)
^
../../include/zen/macros.h:117:36: note: expanded from macro 'ZEN_REPEAT_WITH'
#define ZEN_REPEAT_WITH(s,n,m,...) ZEN_CONCAT(ZEN_REPEAT_WITH_, n)(s,m,__VA_ARGS__)
^
note: (skipping 1 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
../../include/zen/macros.h:67:31: note: expanded from macro 'ZEN_CONCAT_IMPL'
#define ZEN_CONCAT_IMPL(a, b) a ## b
^
<scratch space>:3:1: note: expanded from here
ZEN_REPEAT_WITH_3
^
../../include/zen/macros.h:100:76: note: expanded from macro 'ZEN_REPEAT_WITH_3'
#define ZEN_REPEAT_WITH_3(s,m,...) m(0,__VA_ARGS__) s m(1,__VA_ARGS__) s m(2,__VA_ARGS__)

以下是我想出的邪恶之处:

#define ZEN_FOR_EACH_IMPL(i, m, ...) m(i, ZEN_GET_VA_ARG(i,__VA_ARGS__))
#define ZEN_FOR_EACH_WITH(s, m, ...) ZEN_REPEAT_WITH(s, ZEN_VA_LENGTH(__VA_ARGS__), ZEN_FOR_EACH_IMPL, m, __VA_ARGS__)
#define ZEN_FOR_EACH(m, ...) ZEN_FOR_EACH_WITH(, m, __VA_ARGS__)

它应该允许使用另一个函数宏作为回调对参数列表执行索引遍历,如下所示:

#define DECLARE_VARS_NUMBERED(i, name) int name ## i = 42;
ZEN_FOR_EACH(DECLARE_VARS_NUMBERED, foo, bar, baz);

将生成以下代码:

int foo0 = 42;
int bar1 = 42;
int baz2 = 42;

这个例子不是我打算使用它的目的,但它应该清楚地说明宏的作用。

这些是一些额外的帮助宏。其中大多数是自动生成的。它们的含义应该很简单。我已经在一些简单的用例上测试了它们,它们按预期工作:


#define ZEN_CONCAT(a, b) ZEN_CONCAT_IMPL(a, b)

#define ZEN_CONCAT_IMPL(a, b) a ## b

#define ZEN_REPEAT_WITH_0(s,m,...)
#define ZEN_REPEAT_WITH_1(s,m,...) m(0,__VA_ARGS__)
#define ZEN_REPEAT_WITH_2(s,m,...) m(0,__VA_ARGS__) s m(1,__VA_ARGS__)
#define ZEN_REPEAT_WITH_3(s,m,...) m(0,__VA_ARGS__) s m(1,__VA_ARGS__) s m(2,__VA_ARGS__)
#define ZEN_REPEAT_WITH_4(s,m,...) m(0,__VA_ARGS__) s m(1,__VA_ARGS__) s m(2,__VA_ARGS__) s m(3,__VA_ARGS__)
#define ZEN_REPEAT_WITH_5(s,m,...) m(0,__VA_ARGS__) s m(1,__VA_ARGS__) s m(2,__VA_ARGS__) s m(3,__VA_ARGS__) s m(4,__VA_ARGS__)
#define ZEN_REPEAT_WITH_6(s,m,...) m(0,__VA_ARGS__) s m(1,__VA_ARGS__) s m(2,__VA_ARGS__) s m(3,__VA_ARGS__) s m(4,__VA_ARGS__) s m(5,__VA_ARGS__)
#define ZEN_REPEAT_WITH_7(s,m,...) m(0,__VA_ARGS__) s m(1,__VA_ARGS__) s m(2,__VA_ARGS__) s m(3,__VA_ARGS__) s m(4,__VA_ARGS__) s m(5,__VA_ARGS__) s m(6,__VA_ARGS__)
#define ZEN_REPEAT_WITH_8(s,m,...) m(0,__VA_ARGS__) s m(1,__VA_ARGS__) s m(2,__VA_ARGS__) s m(3,__VA_ARGS__) s m(4,__VA_ARGS__) s m(5,__VA_ARGS__) s m(6,__VA_ARGS__) s m(7,__VA_ARGS__)
#define ZEN_REPEAT_WITH_9(s,m,...) m(0,__VA_ARGS__) s m(1,__VA_ARGS__) s m(2,__VA_ARGS__) s m(3,__VA_ARGS__) s m(4,__VA_ARGS__) s m(5,__VA_ARGS__) s m(6,__VA_ARGS__) s m(7,__VA_ARGS__) s m(8,__VA_ARGS__)

#define ZEN_REPEAT_WITH(s,n,m,...) ZEN_CONCAT(ZEN_REPEAT_WITH_, n)(s,m,__VA_ARGS__)
#define ZEN_REPEAT(n,m,...) ZEN_REPEAT_WITH(,n,m,__VA_ARGS__)

#define ZEN_GET_VA_ARG_0(arg0,...) arg0
#define ZEN_GET_VA_ARG_1(arg0, arg1,...) arg1
#define ZEN_GET_VA_ARG_2(arg0, arg1, arg2,...) arg2
#define ZEN_GET_VA_ARG_3(arg0, arg1, arg2, arg3,...) arg3
#define ZEN_GET_VA_ARG_4(arg0, arg1, arg2, arg3, arg4,...) arg4
#define ZEN_GET_VA_ARG_5(arg0, arg1, arg2, arg3, arg4, arg5,...) arg5
#define ZEN_GET_VA_ARG_6(arg0, arg1, arg2, arg3, arg4, arg5, arg6,...) arg6
#define ZEN_GET_VA_ARG_7(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7,...) arg7
#define ZEN_GET_VA_ARG_8(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8,...) arg8
#define ZEN_GET_VA_ARG_9(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9,...) arg9

#define ZEN_GET_VA_ARG(n,...) ZEN_CONCAT(ZEN_GET_VA_ARG_, n)(__VA_ARGS__)

我真的没有收到错误消息。我猜测这与宏在传递之前被扩展有关,但尝试一些修改并没有产生任何重要的结果。

我找到了this answer但我不确定它如何适用于我的用例。

最佳答案

终于找到问题了😄

该错误是由于 DECLARE_VARS_NUMBERED 的早期扩展造成的,它引入了 ## 特殊运算符,从而禁用了 ZEN_GET_VA_ARG(i, __VA_ARGS__)。将原始代码修改为以下内容即可达到目的:

#define ZEN_FOR_EACH_IMPL(i, m, ...) ZEN_FOR_EACH_IMPL2(i, m, ZEN_GET_VA_ARG(i, __VA_ARGS__))
#define ZEN_FOR_EACH_IMPL2(i, m, arg) m(i, arg)
#define ZEN_FOR_EACH_WITH(s, m, ...) ZEN_REPEAT_WITH(s, ZEN_VA_LENGTH(__VA_ARGS__), ZEN_FOR_EACH_IMPL, m, __VA_ARGS__)
#define ZEN_FOR_EACH(m, ...) ZEN_FOR_EACH_WITH(, m, __VA_ARGS__)
#define ZEN_FOR_EACH_ENUM(m, ...) ZEN_ENUM(ZEN_VA_LENGTH(__VA_ARGS__), ZEN_FOR_EACH_IMPL, m, __VA_ARGS__)

感谢 Eclipse 提供了一个宏扩展,使我能够解决问题。

关于c++ - 为什么我的宏参数之一被替换为 ')' 而不是标识符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58671304/

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