数字转换在宏字符串 [英] convert digital to string in macro

查看:123
本文介绍了数字转换在宏字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  转换一个preprocessor令牌字符串


 的#define NUM 1234

我要定义基于NUM是为const char *,样品中这将是:

 的#define num_str1234

我可以写一个宏语句来实现这一目标?
注:1234将被改变。

感谢。


解决方案

当然可以,但宏替换可以得到一点点奇怪的。双宏替换是有原因的,如果你想想看一段时间,为什么需要它,它就会变得清晰起来。

 的#define STRINGIZER_(EXP)#exp
#定义STRINGIZER(EXP)STRINGIZER_(EXP)
NUM的#define 1234INT主(INT ARGC,CHAR *的argv [])
{
    为const char * p = STRINGIZER(NUM);
    的printf(%S \\ n,p)的;
    返回EXIT_SUCCESS;
}

运行以下命令:

  1234

究其原因,双重替代:乍一看,人们可能认为这将解决这个问题:

 的#define STRINGIZER(EXP)#exp
NUM的#define 1234INT主(INT ARGC,CHAR *的argv [])
{
    为const char * p = STRINGIZER(NUM);
    的printf(%S \\ n,p)的;
    返回EXIT_SUCCESS;
}

但是,这会产生:

  NUM

这是不是我们所试图做的。如果你想在NUM宏观第一的实际扩张那么这是你必须做的:迫使扩张。通过迫使preprocessor替代的第一个的通过中间膨胀(如我在这个答案的顶部)传入的宏第一次扩大,然后的字符串-ized。

边栏::该技术是用于生成,否则定期举行字符串predefined preprocessor宏宽字符版本特别有用。例如, __ __ FILE 宏。假设你想这个宽字符版本(字符串prepended以'L')你可能首先想到这会工作:

 的#define WIDESTR(STR)长## STR

__ __ FILE这个扩展如:

 常量WCHAR * P = WIDESTR(__ FILE__);

将导致编译器错误:未定义的标识符:L__FILE __

那么,如何才能解决这个问题?用同样的方法我们在上面做了。

 的#define WIDESTR_(STR)长## STR
#定义WIDESTR(STR)WIDESTR_(STR)INT主(INT ARGC,CHAR *的argv [])
{
    常量为wchar_t * P = WIDESTR(__ FILE__);
    wcout<< P<< ENDL;
    返回EXIT_SUCCESS;
}

在我的系统,这将产生:

  /Users/craig/tmp/main/main/test.cpp

最后...

作为安慰奖,我们在这个答案都结合成一个巨大咕堆,我们怎么想发生在我们做的这个的:

  INT的main()
{
    常量为wchar_t * P = WIDESTR(STRINGIZE(NUM));
    wcout<< P<< ENDL;
    返回EXIST_SUCCESS;
}

Possible Duplicate:
Convert a preprocessor token to a string

#define num 1234

I want to define a "const char*" based on num, in the sample it would be:

#define num_str "1234"

Can I write a macro statement to achieve this? NB: 1234 would be changed.

Thanks.

解决方案

Yes you can, but the macro substitution can get a little strange-looking. The double-macro substitution is there for a reason, and if you think about it for awhile, it will become clear why it is needed.

#define STRINGIZER_(exp)   #exp
#define STRINGIZER(exp)    STRINGIZER_(exp)
#define NUM 1234

int main(int argc, char *argv[])
{
    const char *p = STRINGIZER(NUM);
    printf("%s\n",p);
    return EXIT_SUCCESS;
}

Running this:

1234

The reason for the double substitution: At first glance one may think this will solve the problem:

#define STRINGIZER(exp)    #exp
#define NUM 1234

int main(int argc, char *argv[])
{
    const char *p = STRINGIZER(NUM);
    printf("%s\n",p);
    return EXIT_SUCCESS;
}

But this produces:

NUM

which is not what we're trying to do. If you want the actual expansion of the NUM macro first then that is what you have to do: force the expansion. By forcing the preprocessor to substitute first through an intermediate expansion (as I show at the top of this answer) the passed-in macro is expanded first, then string-ized.

Side Bar: This technique is particularly useful for generating wide-char versions of predefined preprocessor macros that otherwise hold regular strings. For example, the __FILE__ macro. Suppose you wanted a wide-char version of this (a string prepended with 'L') You may first think this will work:

#define WIDESTR(str)    L##str

but expanding this with __FILE__ as in:

const wchar *p = WIDESTR(__FILE__);

will result in a compiler error: "Undefined identifier: L__FILE__"

So how can we address this? The same way we did above.

#define WIDESTR_(str)       L##str
#define WIDESTR(str)        WIDESTR_(str)

int main(int argc, char *argv[])
{
    const wchar_t* p = WIDESTR(__FILE__);
    wcout << p << endl;
    return EXIT_SUCCESS;
}

On my system, this produces:

/Users/craig/tmp/main/main/test.cpp

In Closing...

As a consolation prize, we combine everything in this answer into one giant goo-pile, what do we suppose happens when we do this:

int main()
{
    const wchar_t *p = WIDESTR(STRINGIZE(NUM));
    wcout << p << endl;
    return EXIST_SUCCESS;
}

这篇关于数字转换在宏字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆