C++ CPP:转义宏标识符

C++ CPP:转义宏标识符,c++,macros,C++,Macros,有没有办法在c预处理器(cpp)中转义宏名称(标识符) 我想用可读的宏名称对一些web代码(html、css…)进行条件化 条件css文件的示例: /*root*/ some rootcode #if height==480 /* height 480 */ .page { line-height:23px; } #elif height<480 /* height < 480 */ .page { line-height:46px; } #endif 结果(

有没有办法在c预处理器(cpp)中转义宏名称(标识符)

我想用可读的宏名称对一些web代码(html、css…)进行条件化

条件css文件的示例:

/*root*/
some rootcode

#if height==480
/* height 480 */
.page {
    line-height:23px;
}

#elif height<480
/* height < 480 */
.page {
    line-height:46px;
}

#endif
结果(删除换行后)

但是“line-480”是错误的

有没有办法在不更改宏名称或字符串化的情况下转义代码中的“高度”?

您可以:

1) 取消定义宏:

#undef height
2) 使用类似标准的大写字母重命名宏:

#define HEIGHT
3) 在处理文件之前使用防护:

#if height==480
#define HEIGHT_480
#undef height
#endif

#if height>480
#define HEIGHT_OVER_480
#undef height
#endif

/*root*/
some rootcode

#if HEIGHT_480
/* height 480 */
.page {
    line-height:23px;
}

#elif HEIGHT_OVER_480
/* height < 480 */
.page {
    line-height:46px;
}

#endif
#如果高度==480
#定义高度
#未定义高度
#恩迪夫
#如果高度>480
#定义高度超过480
#未定义高度
#恩迪夫
/*根*/
一些根代码
#如果高度为480
/*高度480*/
.第页{
线高:23px;
}
#elif高度超过480
/*高度<480*/
.第页{
线高:46px;
}
#恩迪夫
第一个在取消定义后丢失信息。如果大量使用宏,则第二种方法是不切实际的


第三个是IMO的最佳选择。我在生产代码中看到过这样的东西是必要的。

我利用Luchian Grigore的思想来取消使用的文件名的定义,我找到了一个(几乎)通用的解决方案:

在条件的开头包含一个“define.file”,在给定条件之后包含一个“undfine.file”

因此,问题归结为两个宏名称,它们必须保留:DEFINEFILE和UNDEFINEFILE。但是这两个宏可以使用hashcode或randomname进行加密,以避免在条件文本中使用此名称

“define.file”文件:

“unfine.file”

“conditionalcss.ccss”

有了这个想法,重新训练的“out.css”看起来像:

some rootcode
.page {
    line-height:23px;
}
os is windows (if 1 refers to windows)
and height also undefined i hope
此解决方案只有两个宏的缺点,并且由于多次导入,可能会导致性能不佳

我希望它能帮助其他人解决他们的问题

格里茨
肾上腺

这是大多数宏都使用大写字母的原因之一。但将宏标识符大写并不能解决一般问题,只能将其最小化。想象一个宏标识符“a”和一个带有文本的html站点。然后,每个A都将替换为宏的值!通过使用大写和下划线装饰等技巧,您可以解决您的问题,即使解决方案与您在问题中描述的不同。我认为CPP不是完成此任务的好工具。祝贺您:这是我见过的CPP最可疑的用法之一。我简化了一点上下文。取消定义不是选项,因为cpp调用应该在运行时进行,并且(在每个新的ifdef之前)使用static includes进行重新定义是不可能的。资本化可以解决所有基于文本的文档中的一些问题,但不是所有问题。守卫是个好主意,但是我们必须检查,所有代码片段都没有使用这个新名称。尽量减少失败次数是个好主意,但不是解决一般问题的办法。@adremus我不明白。运行时与预处理器定义(宏)无关。这些都是在编译之前处理的,更不用说运行时了。一种可能的解决方案是在每个#if或#elif前面包含一个“define.file”(#define height=1,#define os=2),在这一行后面包含一个“undef.file”(#undef height,#undef os)。但是这个文件是可变的,并且不总是在同一个地方。@adremum好吧,对于一个像您描述的那样动态的系统,您不能真正拥有任何静态的东西(当然,除非您想要构建一个解析器或其他东西)。我很好奇是否还有其他的解决办法,但我个人现在想不出任何办法。
#if height==480
#define HEIGHT_480
#undef height
#endif

#if height>480
#define HEIGHT_OVER_480
#undef height
#endif

/*root*/
some rootcode

#if HEIGHT_480
/* height 480 */
.page {
    line-height:23px;
}

#elif HEIGHT_OVER_480
/* height < 480 */
.page {
    line-height:46px;
}

#endif
#define height 480
#define os 1
#undef height
#undef os
/*root*/
some rootcode

#include __DEFINEFILENAMEPATH__

#if height==480
#include __UNDEFINEFILENAMEPATH__
/* height 480 */
.page {
    line-height:23px;
}
#include __DEFINEFILENAMEPATH__

#elif height<480
#include __UNDEFINEFILENAMEPATH__
/* height > 480 */
.page {
    line-height:46px;
}
#include __DEFINEFILENAMEPATH__

#endif

#if os==1
#include __UNDEFINEFILENAMEPATH__
os is windows (if 1 refers to windows)
and height also undefined i hope
#endif
cpp -P -D __DEFINEFILENAMEPATH__="\"define.file\"" -D __UNDEFINEFILENAMEPATH__="\"undefine.file\"" -oout.css css.ccss
some rootcode
.page {
    line-height:23px;
}
os is windows (if 1 refers to windows)
and height also undefined i hope