范围内的枚举声明是宏的参数
我正在尝试创建一个以范围作为参数的宏。
我知道,这可能不是一件好事等等
我正在尝试这个,但遇到了预处理器查找逗号和括号的问题......问题出在枚举上。
如何在作为宏参数的范围内声明枚举?
当编译器看到枚举项之间的逗号时,它会将其作为分隔符。
如果您想知道我为什么要这样做,是因为我需要注册我的命名空间和类,对于命名空间,我需要知道它们何时关闭,所以我想创建一个宏,该宏最初调用一个注册的静态函数命名空间,封装其内容,最后调用一个静态函数,从注册表中删除命名空间。
使用宏,编码员可以更轻松地执行此操作,并确保他不会忘记删除括号末尾的名称空间。
谢谢,
乔
编辑:
我想要一个接受范围作为参数的宏:
#define MYMACRO(unkownscope) unknownscope
class MYMACRO({
// please, don't take this code seriously, it is just an example so you can understand my question
});
现在,如果我尝试:
#define MYMACRO(unkownscope) unknownscope
class MYMACRO({
enum {
anything = 1,
everything = 2
};
});
由于枚举内的逗号,它不会编译,因为编译器认为它是宏的分隔符。括号内的逗号不会发生这种情况,例如:
int a(){
int x = anyfunction(1, 2);
}
会正常编译,因为逗号位于双括号内。
抱歉,无法提前解释...我的英语不太好,这些单词一直跳过我=[
Ty 寻求答案!
乔
I am trying to create a macro that takes a scope as a parameter.
I know, it is probably not a good thing etc etc.
I was trying this and got the problem that preprocessor looks for commas and parentheses... the problem is with enum.
How would I declare a enum inside a scope that is a parameter of a macro?
when the compiler see the comma between enum itens, it takes it as a separator.
If you are curious to know why I entered into this, is because I need to register my namespaces and classes, for namespaces I need to know when they are closed, so I was thinking to create a macro that initially calls a static function that register the namespace, encapsulate its contents and finally call a static function that removes the namespace from the registry.
With a macro it would be easier for the coder to do this and make sure he doesn't forget to remove the namespace in the end of the bracket.
Thanks,
Joe
EDIT:
I want a macro that accepts a scope as parameters:
#define MYMACRO(unkownscope) unknownscope
class MYMACRO({
// please, don't take this code seriously, it is just an example so you can understand my question
});
now, if I try:
#define MYMACRO(unkownscope) unknownscope
class MYMACRO({
enum {
anything = 1,
everything = 2
};
});
it won't compile because of the comma inside the enum, because the compiler thinks it is a separator of the macro. It doesn't happen with commas inside parentheses, example:
int a(){
int x = anyfunction(1, 2);
}
would compile normally because the comma is inside a double parentheses.
Sorry for not being able to explain earlier... my english is not that good and the words just keep skipping me =[
Ty for the answers!
Joe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来您正在将预处理器推到超出其愿意的范围。虽然它不是那么优雅,但如何将宏分成两部分(一个前置宏和一个后置宏),而不是传递“范围”作为参数,而是用前置宏和后置宏包围您的范围。
所以,如果你的宏看起来像这样:
你会这样做:
It sounds like you are pushing the preprocessor beyond where it's willing to go. While it's not as elegant, how about breaking your macro in two (one pre- and one post-) and rather then passing a "scope" as parameter, you surround your scope with you pre- and post- macros.
So, if your macro looks something like:
You would instead do something like:
您正在尝试使用宏复制 RAII。
我不知道你在谈论枚举什么。
You are attempting to replicate RAII with a macro.
I have no idea what you are talking about with regard to enums.
您已经注意到问题所在, 上的一篇 文章 boostpro.com 总结了问题。
有解决方法,但我会选择使用 Boost.Preprocessor。
在不确切知道您想要在语法上实现什么的情况下,您可能正在寻找类似的东西(编辑为 PP_SEQ):
You already noticed what the problem is, an article on boostpro.com sums the problem up.
There are work-arounds, but i'd go for utilizing Boost.Preprocessor.
Without knowing exactly what you're trying to achieve syntactically, something like this might be what you are looking for (edited to PP_SEQ):