【发布时间】:2019-11-02 12:53:21
【问题描述】:
我正在为 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,但我不确定它如何应用于我的用例。
【问题讨论】:
-
为什么是宏?适当的函数、可变参数模板等怎么样?
-
因为我正在尝试创建一种新语法,它与以下库非常相似:github.com/solodon4/Mach7。从本质上讲,它需要自动生成大量变量等。
-
实际上,使用模板做同样的事情的可能性很小......我实际上从未想过,因为我正忙于剖析 Mach7 的内部结构。
标签: c++ c c-preprocessor metaprogramming