强制预处理器在重新定义中使用以前的定义
更新3:
没关系。我有点得到了我想要的东西。下面给出了类内的唯一标识符。
static const int _counter_start = __COUNTER__;
static const int val1 = __COUNTER__ - _counter_start;
static const int val2 = __COUNTER__ - _counter_start;
更新 2:
我将使用此功能实现类似于消息映射的东西。
class a
{
...
MAP_BEGIN()
MAP_DECL...
MAP_END()
...
};
问题是,对于每个 MAP_DECL,我需要在 2 个位置扩展宏。
class a
{
virtual void func()
{
...
//does something with the decl declaration
}
...
//also expand some stuff here
}
Boost 预处理器(理论上)应该允许我将 MAP_DECL 累积到一个序列中,并在最后将其扩展为 func() (同时扩展类字段)。
更新 1:
我目前正在使用 Boost 预处理器库。目前,每次我需要向序列添加某些内容时,我都必须创建一个新的宏变量/定义,如下所示。
我正在尝试扩展 Boost 预处理器序列,但目前我无法执行此操作
#define SEQ (w)(x)(y)(z)
#define SEQ2 BOOST_PP_SEQ_PUSH_BACK(SEQ, a)
原始:
假设我有以下代码
#define CUR 2
#define CUR CUR + 2
如何强制第二行使用第一行中的 CUR 值线?
Update 3:
Never mind. I kinda got what I was looking for. The following gives unique identifiers inside a class.
static const int _counter_start = __COUNTER__;
static const int val1 = __COUNTER__ - _counter_start;
static const int val2 = __COUNTER__ - _counter_start;
Update 2:
I will be implementing something akin to a message map with this functionality.
class a
{
...
MAP_BEGIN()
MAP_DECL...
MAP_END()
...
};
The thing is, for each MAP_DECL, I need to expand the macro in 2 places.
class a
{
virtual void func()
{
...
//does something with the decl declaration
}
...
//also expand some stuff here
}
Boost preprocessor should (theoretically) allow me to accumulate MAP_DECL into a sequence and expand it into func() at the end (while simultaneously expanding the class fields as we go).
Update 1:
I'm using the Boost Preprocessor library at the moment. I'm currently stuck creating a new macro variable/definition like the following every time I need to add something to the sequence.
I'm trying to expand Boost Preprocessor sequences and I'm stuck doing this at the moment
#define SEQ (w)(x)(y)(z)
#define SEQ2 BOOST_PP_SEQ_PUSH_BACK(SEQ, a)
Original:
Suppose I have the following code
#define CUR 2
#define CUR CUR + 2
How do I force the 2nd line to use the value of CUR from the first line?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简而言之,你不能。
当 CUR 扩展时(在第二个
#define
之后),预处理器将用 CUR + 2 替换 CUR 的实例,并“蓝色绘制”名称 CUR(不再进一步扩展) 。此后,C 编译器会看到 CUR + 2,这很可能会产生编译错误。Succinctly, you can't.
At the time when CUR is expanded (after the second
#define
), the preprocessor will replace an instance of CUR with CUR + 2, and 'blue paint' the name CUR (not expanding it any further). Thereafter, the C compiler sees CUR + 2, which most likely yields a compilation error.即使你能做到这一点,也是危险的。
例子:
Even if you could do that, it's dangerous.
Example:
你不能这样做,你只能 #define 每个宏一次,否则编译器会溢出错误。
You can't do that, you can only #define each macro once, else the compiler will spill an error.
我尝试在某一时刻做类似的事情(附加到增强预处理器序列)。我碰壁了(因为乔纳森在他的回答中所说的),最终做了完全不同的事情。
后来我发现 boost MPL 类型序列具有相同的有效限制(这完全有道理,但有时你不会看到墙,直到你真正撞到它:))。
您几乎必须定义整个序列或给它不同的名称。
您可能正在定义一些成员变量,然后在函数中使用它进行操作(注册?)。如果将消息保存在容器中,则可以让 MAP_DECL 将消息添加到其中,然后在函数中执行 for 循环。
如果每条消息都是单独的类型,请尝试类型擦除(boost::any)或让它们继承某个基类并存储它。
I tried doing something similar at one point (appending to boost preprocessor sequences). I hit the wall hard (because of what Jonathan said in his answer) and ended up doing completely different thing.
Later I found that boost MPL type sequences have same effective limitation (which totally makes sense, but sometimes you don't see a wall until you actually bump into it :)).
You pretty much must define whole sequence or give it different name.
You are probably defining some member variable and then doing things with it in the function (registering?). If you keep messages in a container, you can make your MAP_DECL add a message into it, and then in the function just do a for loop.
If each message is of separate type, try type erasure (boost::any) or make them inherit from some base class and store that.