在编译时宏中增加一个数字 [英] Increment a number in compile time macro

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

问题描述

开关(i){

案例1:

休息;

案例2:

休息;

[...]

案例N:

休息;


}


现在,经过一段时间我想在案例1和案例

2之间添加一个案例2,结果意味着案例2之后的每个案例编号都需要将

增加1,它有一个宏观技巧让生活更轻松吗?这样的

为:


开关(i){

案例AUTO_INC_MACRO:

休息;

案例AUTO_INC_MACRO:

....


提前致谢。


Bin

switch(i) {
case 1:
break;
case 2:
break;
[...]
case N:
break;

}

Now, after some time I want to add a ''case 2'' between case 1 and case
2, the result means every case number after case 2 need to be
incremented by 1, it there a macro tricks to make life easier? Such
as:

switch(i) {
case AUTO_INC_MACRO:
break;
case AUTO_INC_MACRO:
....

Thanks in advance.

Bin

推荐答案

Bin Chen写道:
Bin Chen wrote:

switch(i){

案例1:

休息;

案例2:

休息;

[.. 。]

案例N:

休息;


}


现在,一段时间后我想在案例1和案例

2之间添加案例2,结果意味着案例2之后的每个案例编号都需要增加

1,它有一个宏观技巧,让生活更轻松?这样的

as:
switch(i) {
case 1:
break;
case 2:
break;
[...]
case N:
break;

}

Now, after some time I want to add a ''case 2'' between case 1 and case
2, the result means every case number after case 2 need to be
incremented by 1, it there a macro tricks to make life easier? Such
as:



使用枚举。永远不要使用魔法数字。


-

Ian Collins。

Use an enum. Never use magic numbers.

--
Ian Collins.


陈斌说:
Bin Chen said:

switch(i){

案例1:

休息;

案例2:

休息;

[...]

案例N:

休息;


}


现在,经过一段时间我想在案例1和案例之间添加一个案例2

2,结果意味着案例2之后的每个案例编号都需要增加1,它有一个宏观技巧让生活更轻松吗?这样的

为:


开关(i){

案例AUTO_INC_MACRO:

休息;

案例AUTO_INC_MACRO:

...
switch(i) {
case 1:
break;
case 2:
break;
[...]
case N:
break;

}

Now, after some time I want to add a ''case 2'' between case 1 and case
2, the result means every case number after case 2 need to be
incremented by 1, it there a macro tricks to make life easier? Such
as:

switch(i) {
case AUTO_INC_MACRO:
break;
case AUTO_INC_MACRO:
...



我扫描了伊恩的回答(使用枚举确实!哦,亲爱的我......),

我立刻想到了自己,哦,我觉得我能做得更好

比那个 - Ian'a chap chap chap chap but but but but but but but but but but but $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b真正的C黑客,是吗?然后,正确的是,我通过适当的方式阅读了问题

,这样我就可以通过提供最好的建议和最多来展示我惊人的巫术C技能

有用的洞察力。

所以 - 你准备好了我惊人的智慧之珠吗?好。请仔细聆听

,然后...


使用枚举。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

I scanned Ian''s answer ("Use an enum", indeed! Ohhhhhh deary deary me...),
and I immediately thought to myself, "oh, I reckon I can do a lot better
than that - Ian''s a nice enough chap but he''s really a C++ guy at heart,
and he doesn''t understand all the subtle and clever options available to
the real C hacker, does he?" Then, as is only proper, I read the question
through properly, so that I could show off my astounding wizardly C skills
by giving the best possible advice and the most helpful possible insight.
So - are you ready for my astounding pearl of wisdom? Good. Listen
carefully, then...

Use an enum.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


2008年9月6日星期六23:54:08 -0700(PDT),陈斌

< bi *********@gmail.com写道:
On Sat, 6 Sep 2008 23:54:08 -0700 (PDT), Bin Chen
<bi*********@gmail.comwrote:

> switch(i){
案例1:

休息;
案例2:

休息;
[...]
案例N:

休息;

}

现在,经过一段时间我想在案例1和案例
2之间添加一个案例2,结果意味着案例2之后的每个案例编号需要增加1,它有一个宏观技巧,让生活更轻松?如下:

开关(i){
案例AUTO_INC_MACRO:

休息;
案例AUTO_INC_MACRO:
>switch(i) {
case 1:
break;
case 2:
break;
[...]
case N:
break;

}

Now, after some time I want to add a ''case 2'' between case 1 and case
2, the result means every case number after case 2 need to be
incremented by 1, it there a macro tricks to make life easier? Such
as:

switch(i) {
case AUTO_INC_MACRO:
break;
case AUTO_INC_MACRO:



宏不执行计算。他们只执行文字

替换。然后在代码中随后出现一个宏

将无法访问先前出现的结果。


然而,你可以做你想做的事情用枚举。形式的东西

enum {CASE1 = 1,CASE2,...,CASEN};

并将每个案例标签更改为

case CASE1:

...

案例CASE2:




后来,当你想在CASE1和CASE2之间插入一个案例,更改

你枚举为

enum {CASE1 = 1,CASE1A,CASE2,...,CASEN};

并在CASE2标签之前插入新的代码块。


在六次插入后消除猖獗的混乱

CASE6可能是7而CASE10是13,我建议使用标识符

,它们暗示要执行的功能而不是它们的

数值。比如

enum {INSERT = 1,DELETE,EDIT,COMPUTE};

然后你可以将它修改为

enum {INSERT = 1,删除,编辑,打印,计算};


-

删除电子邮件的del

Macros don''t perform calculations. They only perform text
substitution. And a subsequent appearance of a macro in your code
will not have access to the result of a previous appearance.

However, you can do what you want with an enum. Something of the form
enum {CASE1=1, CASE2, ..., CASEN};
and change each case label to
case CASE1:
...
case CASE2:
etc.

Later, when you want to insert a case between CASE1 and CASE2, change
you enum to
enum {CASE1=1, CASE1A, CASE2, ..., CASEN};
and insert the new block of code immediately before the CASE2 label.

To eliminate the rampant confusion after a half-dozen insertions where
CASE6 may be 7 while CASE10 is 13, I would suggest using identifiers
that are suggestive of the function to be performed rather than their
numeric value. Something like
enum {INSERT=1, DELETE, EDIT, COMPUTE};
and then you could later amend it to
enum {INSERT=1, DELETE, EDIT, PRINT, COMPUTE};

--
Remove del for email


这篇关于在编译时宏中增加一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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