单引一个宏参数 [英] single-quoting a macro argument
本文介绍了单引一个宏参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以编写一个单引号参数的宏?
#define SOME_MACRO(x)
这样SOME_MACRO (foo)扩展为''foo''
谢谢,
Marius
Is it possible to write a macro that single-quotes its argument?
#define SOME_MACRO(x)
such that SOME_MACRO(foo) expands to ''foo''
Thanks,
Marius
推荐答案
" Marius Lazer" <毫安********** @ gmail.com>写道:
#是否可以编写一个单引号参数的宏?
#
#define SOME_MACRO(x)
#
#这样SOME_MACRO(foo)扩展为''foo''
对于单个角色你可以做
#define LITERAL(x)(*#x)
也许编译器可以优化内存参考。
For多个角色,你或许可以逃脱一些像
#define LITERAL(x)(*((int *)#x" \0\0\0\ 0 \\\\\\\ 0"))
但这是假设编译器如何存储字符串。
除非你需要做一个常数的特殊原因,
你可以做一些像
int literalise(char * s){
int i,v; unsigned char * u =(unsigned char *)s;
for(i = 0,v = 0; u [i]&& i< sizeof v; i ++)
v =(v<< CHAR_BIT)|你[$]
返回v;
}
literalise(" foo");
这也是避免依赖于实现的解释
多字符文字,endianess和内存对齐
问题。
-
SM Ryan http://www.rawbw.com/~wyrmwif/
标题没有规定行为。
"Marius Lazer" <ma**********@gmail.com> wrote:
# Is it possible to write a macro that single-quotes its argument?
#
# #define SOME_MACRO(x)
#
# such that SOME_MACRO(foo) expands to ''foo''
For a single character you can do
#define LITERAL(x) (*#x)
and perhaps the compiler can optimise away the memory reference.
For multiple characters, you might be able to get away with some like
#define LITERAL(x) (*((int*)#x "\0\0\0\0\0\0\0\0"))
but that''s making assumptions about how the compiler stores strings.
Unless there''s particular reason you need to make a constant,
you can do something like
int literalise(char *s) {
int i,v; unsigned char *u = (unsigned char*)s;
for (i=0,v=0; u[i] && i<sizeof v; i++)
v = (v<<CHAR_BIT) | u[i]
return v;
}
literalise("foo");
which also avoids implementation dependent interpretation of
multi-character literals, endianess, and memory alignment
questions.
--
SM Ryan http://www.rawbw.com/~wyrmwif/
Title does not dictate behaviour.
我的意思是扩展到''foo'',而不是它''第一个字符。
这里没有价值语义。
#define SOME_MACRO(x)#x
SOME_MACRO(foo)扩展为foo但是我需要''foo''
我尝试了类似这样的东西,但是不是我需要的东西:
#define QUOTE ''
#define SOME_MACRO(x)QUOTE x QUOTE
SOME_MACRO(foo)扩展为''foo''(用空格代替''foo' '')。
谢谢,
Marius
I meant literally expansion to ''foo'', not to it''s first character.
There are no value semantics here.
#define SOME_MACRO(x) #x
SOME_MACRO(foo) expands to "foo" but I need ''foo''
I tried something like this which is close but not exactly what I need:
#define QUOTE ''
#define SOME_MACRO(x) QUOTE x QUOTE
SOME_MACRO(foo) expands to '' foo '' (with spaces instead of ''foo'').
Thanks,
Marius
" Marius Lazer" ; <毫安********** @ gmail.com>写道:
"Marius Lazer" <ma**********@gmail.com> writes:
我的意思是扩展到''foo'',而不是它的第一个字符。
这里没有价值语义。
#define SOME_MACRO(x)#x
SOME_MACRO(foo)扩展为foo但是我需要''foo''
I meant literally expansion to ''foo'', not to it''s first character.
There are no value semantics here.
#define SOME_MACRO(x) #x
SOME_MACRO(foo) expands to "foo" but I need ''foo''
我认为你不能从C预处理器中得到你想要的东西。
- -
我没有那个方便的C& V,但我有Dan Pop。
--E。 Gibbons
I don''t think you can get what you want from the C preprocessor.
--
"I don''t have C&V for that handy, but I''ve got Dan Pop."
--E. Gibbons
这篇关于单引一个宏参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文