Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

C multi line macro: do/while(0) vs scope block

http://docs.google.com/View?docid=ajbgz6fp3pjh_2dwwwwt#_38239340844832237
It is not about optimization.
The whole idea of using 'do/while' version
is to make a macro which will
expand into a regular statement, not into a
compound statement. This is
done in order to make the use of function-style
macros uniform with the
use of ordinary functions in all
contexts.
Consider the following code sketch
if
(<condition>)
foo(a);
else
bar(a);
where 'foo' and 'bar'
are ordinary functions. Now imagine that you'd
like to replace function 'foo'
with a macro of the above nature
if
(<condition>)
CALL_FUNCS(a);
else
bar(a);
Now, if your
macro is defined in accordance with the second approach
(just '{' and '}')
the code will no longer compile, because the 'true'
branch of 'i' is now
represented by a compound statement. And when you
put a ';' after this
compound statement, you finished the whole 'if'
statement, thus orphaning the
'else' branch (hence the compilation error).
One way to correct this
problem is to remember not to put ';' after
macro "invocations"
if
(<condition>)
CALL_FUNCS(a)
else
bar(a);
This will compile
and work as expected, but this is not uniform. The
more elegant solution is
to make sure that macro expand into a regular
statement, not into a compound
one. One way to achieve that is to define
the macro as follows
#define
CALL_FUNCS(x) \
do { \
func1(x); \
func2(x); \
func3(x); \
}
while (0)
Now this code
if
(<condition>)
CALL_FUNCS(a);
else
bar(a);
will compile
without any problems.
However, note the small but important difference
between my definition
of 'CALL_FUNCS' and the first version in your message.
I didn't put a
';' after '} while (0)'. Putting a ';' at the end of that
definition
would immediately defeat the entire point of using 'do/while' and
make
that macro pretty much equivalent to the compound-statement version.


Ïà¹ØÎĵµ£º

¹ØÓÚ¡¶0 bug C/C++ÉÌÓù¤³ÌÖ®µÀ¡·µÄÒ»´¦bug

ÕâÁ½ÌìÓкܶàÅóÓÑÒѾ­ÂòÁËÊéÁË£¬²¢ÇÒ¿ªÊ¼¿´£¬ºÇºÇ£¬ÎÒÐÄÀïÒ²ºÜ¸ßÐË¡£
àÅ£¬ÒªËµ½­ºþÉϲØÁúÎÔ»¢ÄØ£¬Õâ²»£¬Õâ²Å¼¸Ììʱ¼ä£¬ÒѾ­ÓÐÅóÓÑÖ¸³öÎÒÊéÖеÄÒ»´¦Ã÷ÏÔ´íÎó£¬ÕâÀïÎÒÕýʽ¸ø´ó¼Ò˵Ã÷һϣ¬ÃâµÃ¶Ô¸÷λ¶ÁÕßÓиö²»ºÃµÄÎ󵼡£
ÎÊÌâ³öÔÚµÚ26Ò³µÄÒ»¸öͼÒÔ¼°ÆäÏà¹ØÎÄ×Ö¡£ÕâÊǵڶþÕ»ù´¡ÖªÊ¶µÄµÚÒ»½Ú£¬Æäʵ¾ÍÊǹØÓÚÄÚ´æµÄ½²½â£¬´ó¼Ò ......

C ÓïÑÔÈëÃÅÖ¸µ¼

C ÓïÑÔ
  
CÓïÑÔÊÇÒ»ÖÖ¼ÆËã»úÓïÑÔ£¬¼òµ¥µÄ½²ËüÊÇÎÒÃǺͼÆËã»ú¹µÍ¨µÄÒ»ÖÖ¹¤¾ß£¬Í¨¹ýÕâÖÖÓïÑÔÎÒÃÇ¿ÉÒÔÒýµ¼¼ÆËã»úÈÃËü°´ÕÕÎÒÃǵÄÒâԸȥ×öÊ£¬À´ÎªÈËÀàÌṩ·þÎñ¡£±ÈÈçÄãµçÄÔÖеÄÒôÀÖ²¥·ÅÆ÷¡¢word¡¢qq¡¢Ñ¸À×£¨µ±È»ÕâЩÈí¼þµÄ±àд»¹Ó¦ÓÃÁËÇÒËû¹¤¾ß£©¡£
CÓïÑÔ¿ÉÒÔ×÷Ϊ¹¤×÷ϵͳÉè¼ÆÓïÑÔ£¬±àдϵͳӦÓóÌÐò£¬Ò²¿ÉÒÔ×÷Î ......

ANSI C¶ÁÊé±Ê¼ÇϵÁÐÖ®×Ö·û¼¯Æª µÚËÄÕ ×Ö½ÚÐò

"endian"Õâ¸ö´Ê³ö×Ô<<¸ñÁзðÓμÇ>>,СÈ˹úµÄÄÚÕ½¾ÍÔ´ÓÚ³Ô¼¦µ°Ê±ÊǾ¿¾¹´Ó´óÍ·(Big-Endian)Çÿª»¹ÊÇ´ÓСͷ(Little-Endian)Çÿª.ÎÒÃÇÒ»°ã½«endian·­Òë³É"×Ö½ÚÐò",½«big endianºÍlittle endian³Æ×÷"´ó¶Ë"ºÍ"С¶Ë".
ÔÚ¼ÆËã»ú¿ÆÑ§ÁìÓòÖÐ,×Ö½ÚÐòÊÇÖ¸´æ·Å¶à×Ö½ÚÊý¾ÝµÄ×Ö½ÚµÄ˳Ðò,µäÐ͵ÄÇé¿öÊÇÕûÊýÔÚÄÚ´æÖеĴæ·Å·½Ê½ºÍ ......

ÓÃFlashÓëCÓïÑÔÖÆ×÷¹¤³ÌʵʱÊý¾Ý¶¯Ì¬ÇúÏßͼ

±¾ÎÄÖ¼ÔÚ˵Ã÷ÈçºÎÀûÓÃFlashºÍCÓïÑÔÖÆ×÷BSģʽϵÄʵʱÊý¾Ý¶¯Ì¬ÇúÏßͼ£¬ÏÖÔÚÁ÷ÐеÄʵʱÊý¾ÝÇúÏßͼ£¬´ó¶¼ÊDzÉÓÃÁËCSģʽµÄ¿ª·¢ÓïÑÔ£¬BSģʽµÄËäÓÐһЩʵÀý£¬±ÈÈçgoogleÉϵĹÉÊÐÇúÏßͼ£¬µ«ÆäʵÏֵķ½Ê½ºÍÁ÷³ÌÔÚÍøÉϺÜÉÙÄܼûµ½¡£
ÆäÔËÐÐʱµÄ½çÃæÈçÏ£¬Êý¾Ýʵʱ¸üУ¬ÇúÏßͼ´Ó×óÍùÓÒ¶¯Ì¬Òƶ¯£º
ÏÂÃæÏȽ²ÔÚʵÏÖ¹ý³ÌÖеŤ×÷»·¾³ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ