宏值的字符串化
我遇到了一个问题 - 我需要使用宏值作为字符串和整数。
#define RECORDS_PER_PAGE 10
/*... */
#define REQUEST_RECORDS \
"SELECT Fields FROM Table WHERE Conditions" \
" OFFSET %d * " #RECORDS_PER_PAGE \
" LIMIT " #RECORDS_PER_PAGE ";"
char result_buffer[RECORDS_PER_PAGE][MAX_RECORD_LEN];
/* ...and some more uses of RECORDS_PER_PAGE, elsewhere... */
这会失败并显示有关“stray #”的消息,即使它有效,我想我也会将宏名称字符串化,而不是值。当然,我可以将值提供给最终方法( "LIMIT %d ", page*RECORDS_PER_PAGE
),但它既不漂亮也不高效。 在这种情况下,我希望预处理器不要以特殊方式处理字符串,而是像正常代码一样处理它们的内容。 目前,我用 #define RECORDS_PER_PAGE_TXT "10"
来解决它,但可以理解的是,我对此并不满意。
如何做对呢?
I faced a problem - I need to use a macro value both as string and as integer.
#define RECORDS_PER_PAGE 10
/*... */
#define REQUEST_RECORDS \
"SELECT Fields FROM Table WHERE Conditions" \
" OFFSET %d * " #RECORDS_PER_PAGE \
" LIMIT " #RECORDS_PER_PAGE ";"
char result_buffer[RECORDS_PER_PAGE][MAX_RECORD_LEN];
/* ...and some more uses of RECORDS_PER_PAGE, elsewhere... */
This fails with a message about "stray #", and even if it worked, I guess I'd get the macro names stringified, not the values. Of course I can feed the values to the final method ( "LIMIT %d ", page*RECORDS_PER_PAGE
) but it's neither pretty nor efficient.
It's times like this when I wish the preprocessor didn't treat strings in a special way and would process their content just like normal code.
For now, I cludged it with #define RECORDS_PER_PAGE_TXT "10"
but understandably, I'm not happy about it.
How to get it right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
下面定义的 xstr 宏将在宏展开后进行字符串化。
The
xstr
macro defined below will stringify after doing macro-expansion.输出:
请注意对 _REQUEST_RECORDS 的间接访问,以在对参数进行字符串化之前评估参数。
Outputs:
Note the indirection to _REQUEST_RECORDS to evaluate the arguments before stringifying them.
尝试双重转义你的报价
为我编译。令牌
RECORDS_PER_PAGE
将通过ESCAPEQUOTE
宏调用进行扩展,然后发送到DOUBLEESCAPE
进行引用。Try double escaping your quotes
compiles for me. The token
RECORDS_PER_PAGE
will be expanded by theESCAPEQUOTE
macro call, which is then sent intoDOUBLEESCAPE
to be quoted.