本文介绍了重新制定宏只使用一次参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 考虑这个宏 //检查x,假设类型为unsigned char,是否在[0x20..0x7E]范围内 #define ISVALID (x)((x)> = 0x20&&(x)< = 0x7E) 当然,这不能安全地用于 if(ISVALID(* p ++))foo(); 其中p是指针ot unsot char。 除非我错了,这个问题可以使用 #define ISVALID(x)((unsigned char)((x)-0x20)<来修复(通常,性能提高) = 0x7E-0x20) Buf我们可以对此进行类似的操作吗? //检查是否为x,假设类型为unsigned char , //在范围[0x20..0x7E]或大于0xC0 #define ISVALID(x)((x)> = 0x20&& (x)< = 0x7E ||(x)> = 0xC0) Francois Grieu 解决方案 Francois Grieu写道: 考虑这个宏 //检查是否x,假设类型为unsigned char,在[0x20..0x7E]范围内 #define ISVALID(x)((x)> = 0x20&& (x)< = 0x7E) 当然,这不能安全地用作 if(ISVALID(* p ++))foo (); 其中p是指向unsigned char的指针。 所以不要那样使用它。 注意ISVALID是一类保留标识符。 除非我错了,否则这个问题可以修复(通常,性能提高 )使用 #define ISVALID(x)((unsigned char)((x)-0x20)< = 0x7E-0x20) 为什么要这么麻烦? < ctype.h>中的ISXXXX宏也存在同样的问题,但它通常不会出现问题。 我们可以对这个做类似的事情吗? //检查是否为x,假设类型为unsigned char, //在范围内[0x20。 .0x7E]或大于0xC0 #define ISVALID(x)((x)> = 0x20&&(x)< = 0x7E ||(x)> = 0xC0) 如果假设x在0..0xFF的范围内,你可以做... #define VALID (x)(28245449%(((x)/ 32 + 1)* 6 + 5)== 0) - 彼得 文章< 11 ********************** @ 51g2000cwl.googlegroups。 com>, " Peter Nilsson" < ai *** @ acay.com.auwrote: Francois Grieu写道:(名称和评论已修复) //检查是否为x,假设类型为unsigned char, //在[0x20..0x7E]范围内或至少为0xC0 #define VALID(x)((x)> = 0x20&&(x)< = 0x7E ||(x)> = 0xC0) 如果假设x在0..0xFF的范围内,你可以做... #define VALID(x)(28245449%(((x)/ 32 + 1)* 6 + 5)== 0) 那不行。在(x)/ 32之后,0x7E和0x7F是无法区分的。 Francois Grieu " Peter Nilsson" < ai *** @ acay.com.auwrites: Francois Grieu写道: 考虑这个宏 //检查x,假设类型为unsigned char,是否在[0x20..0x7E]范围内 #define ISVALID(x)( (x)> = 0x20&&(x)< = 0x7E) 当然,这不能安全地用作 if(ISVALID(* p ++))foo(); 其中p是指针unsot char。 所以不要那样使用它。 请注意,ISVALID属于一类保留标识符。 不,它不是。 除非我错误,这个问题可以修复(通常,性能提高),使用 #define ISVALID(x)((unsigned char)((x) -0x20)< = 0x7E-0x20) 为什么要这么麻烦?对于 < ctype.h>中的ISXXXX宏存在同样的问题,但它通常不是问题。 < ctype.h>中没有ISXXXX宏。在< ctype.h中声明了许多isXXXX 函数(它们也可以实现为具有相同名称的宏)。 - Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst> 圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst> 我们必须做点什么。这是事情。因此,我们必须这样做。 Consider this macro // check if x, assumed of type unsigned char, is in range [0x20..0x7E]#define ISVALID(x) ((x)>=0x20 && (x)<=0x7E) Of course, this can''t be safely used as inif (ISVALID(*p++)) foo();where p is a pointer ot unsigned char.Unless I err, this issue can be fixed (and often, performanceimproved) using #define ISVALID(x) ((unsigned char)((x)-0x20)<=0x7E-0x20) Buf can we do something similar about this one? // check if x, assumed of type unsigned char,// is in range [0x20..0x7E] or grater than 0xC0#define ISVALID(x) ((x)>=0x20 && (x)<=0x7E || (x)>=0xC0) Francois Grieu 解决方案 Francois Grieu wrote:Consider this macro// check if x, assumed of type unsigned char, is in range [0x20..0x7E]#define ISVALID(x) ((x)>=0x20 && (x)<=0x7E)Of course, this can''t be safely used as in if (ISVALID(*p++)) foo();where p is a pointer ot unsigned char.So don''t use it that way. Note that ISVALID is in a class of reserved identifiers. Unless I err, this issue can be fixed (and often, performanceimproved) using#define ISVALID(x) ((unsigned char)((x)-0x20)<=0x7E-0x20)Why bother? The same issue exists for the ISXXXX macros in<ctype.h>, but it generally _isn''t_ a problem. Buf can we do something similar about this one?// check if x, assumed of type unsigned char,// is in range [0x20..0x7E] or grater than 0xC0#define ISVALID(x) ((x)>=0x20 && (x)<=0x7E || (x)>=0xC0)If you assume x is in the range 0..0xFF, you can do... #define VALID(x) (28245449 % (((x)/32+1)*6+5) == 0) --Peter In article <11**********************@51g2000cwl.googlegroups. com>,"Peter Nilsson" <ai***@acay.com.auwrote: Francois Grieu wrote: (name and comment fixed) // check if x, assumed of type unsigned char, // is in range [0x20..0x7E] or at least 0xC0 #define VALID(x) ((x)>=0x20 && (x)<=0x7E || (x)>=0xC0) If you assume x is in the range 0..0xFF, you can do... #define VALID(x) (28245449 % (((x)/32+1)*6+5) == 0)That wont work. After (x)/32, 0x7E and 0x7F are undistinguishable.Francois Grieu"Peter Nilsson" <ai***@acay.com.auwrites:Francois Grieu wrote: Consider this macro // check if x, assumed of type unsigned char, is in range [0x20..0x7E] #define ISVALID(x) ((x)>=0x20 && (x)<=0x7E) Of course, this can''t be safely used as in if (ISVALID(*p++)) foo(); where p is a pointer ot unsigned char. So don''t use it that way.Note that ISVALID is in a class of reserved identifiers.No, it ISn''t. Unless I err, this issue can be fixed (and often, performance improved) using #define ISVALID(x) ((unsigned char)((x)-0x20)<=0x7E-0x20) Why bother? The same issue exists for the ISXXXX macros in<ctype.h>, but it generally _isn''t_ a problem.There are no ISXXXX macros in <ctype.h>. There are a number of isXXXXfunctions declared in <ctype.h(which may also be implemented asmacros with the same names). --Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>We must do something. This is something. Therefore, we must do this. 这篇关于重新制定宏只使用一次参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 02:02