C++ 条件宏观评价
我有一个全局定义的符号,需要针对源文件的给定子集有条件地取消定义。 所有需要特殊处理的文件都已包含在前包含和后包含中:
pre.h:
#undefine mysymbol // [1]
post.h:
#define mysymbol MY_SYMBOL_DEFINITION // [2]
我的问题是 pre由于各种包含链,.h
和 post.h
可以针对给定源文件多次包含。 因此,我需要 1 第一次发生.h 已包含在内,我需要 2 才能发生上次 包含 post.h
。 从概念上讲:
pre // undefine
pre // no-op
pre // no-op
post // no-op
post // no-op
post // redefine
由于我使用的是 GCC 3.4.6,因此我无法访问 push 和 pop 宏编译指示 可能会为我解决这个问题。
如何使用剩余的预处理器功能来模拟该行为?
我试图用预处理器做一些类似增加/减少值的事情,但我不确定这是否可能。
“我到底想做什么?”
我们有宏将 new
替换为 new(__FILE__, __LINE__)
- 请参阅 我关于这个主题的其他问题 - 我们需要取消定义由上述前包含和后包含包含的源文件集中的那些宏,因为我们无法创建一个与其中使用的放置新语法兼容的宏。
I have a symbol defined globally that needs to be conditionally undefined for a given subset of my source files. All of the files that require special treatment are already wrapped in pre- and post-inclusions:
pre.h:
#undefine mysymbol // [1]
post.h:
#define mysymbol MY_SYMBOL_DEFINITION // [2]
My problem is that the pre.h
and post.h
can be included multiple times for a given source file due to various inclusion chaining. As such, I need 1 to happen the first time pre.h is included and I need 2 to happen the last time that post.h
is included. Conceptually:
pre // undefine
pre // no-op
pre // no-op
post // no-op
post // no-op
post // redefine
Since I am using GCC 3.4.6, I do not have access to the push and pop macro pragmas that might otherwise solve this issue for me.
How can I emulate that behavior with the remaining preprocessor functionality?
I was attempting to do something like increment/decrement a value with the preprocessor, but I'm not sure that's possible.
"What am I really trying to do?"
We have macros to replace new
with new(__FILE__, __LINE__)
-- see my other question on this topic -- and we need to undefine those macros in the set of source files wrapped by the pre- and post-includes described above because we were unable to create a macro that's compatible with the placement new syntax used therein.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将类似这样的内容添加到您的 pre.h 文件中:
在 post.h 中:
但您需要知道您能走多深。
You can add somethig like this to your pre.h file:
And in post.h:
But you need to know how deep can you go.
如果您知道递归的最大深度,那么您应该能够通过在每个级别定义一个新宏来模拟推入/弹出。 对于您给出的 3 级示例,它看起来像:
Pre.h:
Post.h
If you know the maximum depth of recursion, then you should be able to simulate the push/pop by defining a new macro at each level. For the 3 level example you give, that would look something like:
Pre.h:
Post.h
pre.h
post.h
在这种情况下工作正常:
编辑:
与 gcc 4.0 配合良好。
pre.h
post.h
Which works fine in this case:
Edit:
Works fine with gcc 4.0.
通过像 Loki 那样的元编程魔法,可以在编译时递增/递减一个值。 但你确定这是必要的吗?
为什么你的h文件被包含了这么多次? 为什么不使用包含防护?
Incrementing/decrementing a value at compile time is probably possible with meta-programming wizardry a-la Loki. But are you sure this is necessary?
Why is your h file included so many time? Why not use an include guard?
尝试:
文件 pre.h
文件 post.h
使用此代码进行测试
Try:
File pre.h
File post.h
Tested with this code