如何在宏中单引号参数?

发布于 2024-08-17 14:58:42 字数 213 浏览 3 评论 0原文

我想创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

甩你一脸翔 2024-08-24 14:58:42

你能做的最好的事情是

#define Q(x) ((#x)[0])

或者

#define SINGLEQUOTED_A 'A'
#define SINGLEQUOTED_B 'B'
...
#define SINGLEQUOTED_z 'z'

#define Q(x) SINGLEQUOTED_##x

这仅适用于 a-z, A-Z, 0-9_ (对于某些编译器还有 $)。

The best you can do is

#define Q(x) ((#x)[0])

or

#define SINGLEQUOTED_A 'A'
#define SINGLEQUOTED_B 'B'
...
#define SINGLEQUOTED_z 'z'

#define Q(x) SINGLEQUOTED_##x

This only works for a-z, A-Z, 0-9 and _ (and $ for some compilers).

凶凌 2024-08-24 14:58:42

实际上,#X 将其参数用双引号括起来,如以下代码所示。

#define QQ(X) #X
char const * a = QQ(A);

使用 gcc -E 运行此命令(仅查看预处理器输出)以查看

# 1 "temp.c"
# 1 "<built-n>"
# 1 "<command line>"
# 1 "temp.c"

char * a = "A"

To single quote your argument(在C中意味着它是单个字符)使用下标,

#define Q(X) (QQ(X)[0])
char b = Q(B);

它将被转换为

char b = ("B"[0]);

Actually, #X double quotes its argument, as you can see with the following code.

#define QQ(X) #X
char const * a = QQ(A);

Run this with gcc -E (to just see the preprocessor output) to see

# 1 "temp.c"
# 1 "<built-n>"
# 1 "<command line>"
# 1 "temp.c"

char * a = "A"

To single quote your argument (which in C means that it's a single character) use subscripting

#define Q(X) (QQ(X)[0])
char b = Q(B);

which will be transformed into

char b = ("B"[0]);
糖粟与秋泊 2024-08-24 14:58:42

我能想到的最好的办法是,

#define Q(A) (#A[0])

但不可否认,这不是很漂亮。

The best I can think of would be

#define Q(A) (#A[0])

but this isn't very pretty, admittedly.

唱一曲作罢 2024-08-24 14:58:42

这会生成转换:

#python
for i in range(ord('a'), ord('n')):
    print "#define BOOST_PP_CHAR_%s '%s'" % (chr(i), chr(i))

这是预处理器部分:

#ifndef BOOST_PP_CHAR_HPP
#define BOOST_PP_CHAR_HPP

#define BOOST_PP_CHAR(c) BOOST_PP_CHAR_ ## c
//  individual declarations    

#endif // BOOST_PP_CHAR_HPP

this generates conversions:

#python
for i in range(ord('a'), ord('n')):
    print "#define BOOST_PP_CHAR_%s '%s'" % (chr(i), chr(i))

and this is preprocessor part:

#ifndef BOOST_PP_CHAR_HPP
#define BOOST_PP_CHAR_HPP

#define BOOST_PP_CHAR(c) BOOST_PP_CHAR_ ## c
//  individual declarations    

#endif // BOOST_PP_CHAR_HPP
悲歌长辞 2024-08-24 14:58:42

我刚刚尝试过串联:

#define APOS           '
#define CHAR2(a,b,c)   a##b##c
#define CHAR1(a,b,c)   CHAR2(a,b,c)
#define CHAR(x)        CHAR1(APOS,x,APOS)

但不幸的是,预处理器抱怨未终止的字符。 (如果您有多个字符,则为多字符)
一种仅禁用预处理器错误的方法:(对此没有特定的警告选项)

-no-integrated-cpp -Xpreprocessor -w

一些带有其他技巧的编译时优化示例:

#define id1_id       HELP
#define id2_id       OKAY

#define LIST(item,...) \
    item(id1, ##__VA_ARGS__)\
    item(id2, ##__VA_ARGS__)\
    item(id1, ##__VA_ARGS__)\

#define CODE(id,id2,...)    ((CHAR(id##_id) == CHAR(id2##_id)) ? 1 : 0) +
int main() { printf("%d\n", LIST(CODE,id1) 0); return 0; }

这将返回“2”,因为有两个项目具有 id1。

I've just tried concatenation:

#define APOS           '
#define CHAR2(a,b,c)   a##b##c
#define CHAR1(a,b,c)   CHAR2(a,b,c)
#define CHAR(x)        CHAR1(APOS,x,APOS)

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)

-no-integrated-cpp -Xpreprocessor -w

Some compile-time optimization example with some other tricks:

#define id1_id       HELP
#define id2_id       OKAY

#define LIST(item,...) \
    item(id1, ##__VA_ARGS__)\
    item(id2, ##__VA_ARGS__)\
    item(id1, ##__VA_ARGS__)\

#define CODE(id,id2,...)    ((CHAR(id##_id) == CHAR(id2##_id)) ? 1 : 0) +
int main() { printf("%d\n", LIST(CODE,id1) 0); return 0; }

This returns "2", since there are two items that have id1.

笑叹一世浮沉 2024-08-24 14:58:42

经过一些宏的摆弄后,我找到了解决方案。

#define QUOTE_LITERAL() '
#define IN_QUOTES(x) QUOTE_LITERAL()x'

您将收到警告(对于未终止的引号),但可以使用 -no-integrated-cpp -Xpreprocessor -w 消除它们。正如@darkfader 提到的

After some fiddling with macros I found the solution.

#define QUOTE_LITERAL() '
#define IN_QUOTES(x) QUOTE_LITERAL()x'

You will get warnings (for unterminated quotes) but can get rid of them with -no-integrated-cpp -Xpreprocessor -w. As mentioned by @darkfader

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文