如何在宏中单引号参数?
我想创建一个 C 预处理器宏,它将单引号引用参数。就像常用的#X
一样。
我希望将 Q(A)
扩展为 'A'
。
我在 Linux 上使用 gcc。
有人有想法吗?
我知道 #
双引号。我正在寻找类似的单引号机制。
I would like to create a C pre-processor macro that will single-quote the argument. Just like the common used #X
.
I want Q(A)
to be expanded to 'A'
.
I am using gcc on Linux.
Does any one have an idea?
I know #
double-quotes. I am looking for a similar mechanism for single-quoting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你能做的最好的事情是
或者
这仅适用于
a
-z
,A
-Z
,0
-9
和_
(对于某些编译器还有$
)。The best you can do is
or
This only works for
a
-z
,A
-Z
,0
-9
and_
(and$
for some compilers).实际上,
#X
将其参数用双引号括起来,如以下代码所示。使用 gcc -E 运行此命令(仅查看预处理器输出)以查看
To single quote your argument(在C中意味着它是单个字符)使用下标,
它将被转换为
Actually,
#X
double quotes its argument, as you can see with the following code.Run this with
gcc -E
(to just see the preprocessor output) to seeTo single quote your argument (which in C means that it's a single character) use subscripting
which will be transformed into
我能想到的最好的办法是,
但不可否认,这不是很漂亮。
The best I can think of would be
but this isn't very pretty, admittedly.
这会生成转换:
这是预处理器部分:
this generates conversions:
and this is preprocessor part:
我刚刚尝试过串联:
但不幸的是,预处理器抱怨未终止的字符。 (如果您有多个字符,则为多字符)
一种仅禁用预处理器错误的方法:(对此没有特定的警告选项)
一些带有其他技巧的编译时优化示例:
这将返回“2”,因为有两个项目具有 id1。
I've just tried concatenation:
Unfortunately though, the preprocessor complains about an unterminated character. (and multicharacter if you have more than one character)
A way to just disable preprocessor errors: (there is no specific warning option for this)
Some compile-time optimization example with some other tricks:
This returns "2", since there are two items that have id1.
经过一些宏的摆弄后,我找到了解决方案。
您将收到警告(对于未终止的引号),但可以使用
-no-integrated-cpp -Xpreprocessor -w
消除它们。正如@darkfader 提到的After some fiddling with macros I found the solution.
You will get warnings (for unterminated quotes) but can get rid of them with
-no-integrated-cpp -Xpreprocessor -w
. As mentioned by @darkfader