|
|
C++ faq 38.5 - macros with multiple statements
This item in the C++ faq:
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-38.5
discusses macros with multiple statements.
The problem is that a macro such as
#define MYMACRO(a,b) stmt1; stmt2;
will cause unwanted astonishment if you say:
while(whatever)
MYMACRO(foo,bar);
This is also problematic:
#define MYMACRO(a,b) {stmt1; stmt2;}
because this won't compile:
if(foo())
MYMACRO(i,j);
else
baz();
since you can't have "}; else"
so the faq proposes this solution:
-------------
#define MYMACRO(a, b) \
if (true) { \
statement1; \
statement2; \
... \
statementN; \
} else
-------------
But this seems like a bad solution; what if you write this:
MYMACRO(foo,bar) // note no semicolon
baz();
it compiles fine, but baz() *never* gets called!
Is there a way to avoid this? Maybe the do/while idiom should be used
dispite the inlining problems?
// compiler may not inline functions that use this because of the loop...
#define MYMACRO(a, b) \
do { \
statement1; \
statement2; \
... \
statementN; \
} while (false)
thanks,
Erik
|
|
0
|
|
|
|
Reply
|
haugen (2)
|
1/8/2004 10:40:03 AM |
|
"Erik Haugen" <haugen@Xenon.Stanford.EDU> wrote:
> This item in the C++ faq:
> http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-38.5
>
> discusses macros with multiple statements.
[snip]
> the faq proposes this solution:
>
> -------------
> #define MYMACRO(a, b) \
> if (true) { \
> statement1; \
> statement2; \
> ... \
> statementN; \
> } else
> -------------
>
> But this seems like a bad solution; what if you write this:
>
> MYMACRO(foo,bar) // note no semicolon
> baz();
>
> it compiles fine, but baz() *never* gets called!
>
> Is there a way to avoid this? Maybe the do/while idiom should be used
> dispite the inlining problems?
How about this:
#define MYMACRO(a, b) \
if (true) { \
statement1; \
statement2; \
} else true
This makes the null statement explicit, and should cause a syntax error if
no semi colon follows (if this looks weird to anyone, an expression that
does nothing like "true" or "1+3" is a valid statement in C++).
The following cases mentioned in the FAQ should work OK:
while (whatever)
MYMACRO(foo, bar);
if (whatever)
MYMACRO(foo, bar);
else
baz;
And there will be a (possibly obscure !) syntax error if the semi colon is
missing, as in your example:
MYMACRO(foo,bar) // note no semicolon
baz();
- because it will expand to "true baz()" at the end.
The reason for using "true" as the null statement and not "0" is because
there is less chance of the statement still being valid without a semi
colon, eg. something beginning with "-" or "*".
Hope this helps - let me know if I've missed something !
David F
|
|
0
|
|
|
|
Reply
|
nospam5096 (66)
|
1/8/2004 10:53:13 PM
|
|
|
1 Replies
36 Views
(page loaded in 0.053 seconds)
Similiar Articles: Regarding SAS Macro - comp.soft-sys.sas[c] is correct because the %macro statement defines two positional (not keyword ... debugging, design, etc.) by a multiple ... iw1sas: 1/5/2010 5:51:38 PM Nested while loops within if statements - comp.soft-sys.matlab ...... mathworks.com > comp.soft-sys.matlab (CSSM) FAQ ... DOW Loop with multiple sum statements within by-group - comp.soft ... ... Is this a macro bug??? - comp.soft-sys.sas Hi, I ... Assigning same value to multiple variables - comp.lang.rexx ...... challenge was to set two variables in one statement ... 6/3/2008 5:30:38 PM ... How to pass dataset variables' value to macro - comp ... Floating Point and printf() - comp.lang.asm.x86Reply: Frank: 1/17/2006 1:32:38 AM ... Microsoft (R) Macro Assembler Version 5.00 1/16/6 Page ... JK-Technology.Com FAQs for comp.lang.c http://c-faq ... Neatest way to get the end pointer? - comp.lang.c(Oh and by the way, I wouldn't use a macro such as ARRLEN ... in the answer to question 6.17 in the comp.lang.c FAQ ... On Feb 12, 8:38 am, r...@hoekstra-uitgeverij.nl ... FAQ -- assembly-language/x86/general/part1 - comp.lang.asm.x86 ...... FAQ is to go beyond just answering the frequently asked questions. ... prefixed to the subject, rather than multiple 're:'s. ... Disassemblers > 38. How to Optimize for the ... const char ** syntax question - comp.lang.c++.moderated... two independant errors here, one is explained by the FAQ ... the library version (either dynamically or through a macro ... 7/23/2012 1:38:56 PM Wrap a function - comp.lang.pythonThe text file has both Python statements and "special ... comma separated sequence of parameters over multiple ... 2/4/2010 5:38:53 AM Filling paragraph with no wrap - comp.emacsThe Emacs FAQ and what I've found using ... replace-string RET %%%%% RET C-q C-j C-q C-j RET > I'm a writer. I use a macro ... populating multiple checkbox choices in script ... How to extract center of mass dimensions? - comp.cad.solidworks ...If tha macro could be embedded in the > part ... time this part changes (which can be multiple ... grasp, pause and think about the next statement, automatically move the c ... The C Preprocessor: 1. The C Preprocessor... directive there comes a C statement of the form ... is defined if and only if this is GNU C. This macro is ... If the expansion consists of multiple statements, then the ... C++ nested macros? - Stack OverflowIs there any way, in C++, to define nested macros ... Nov 12 '09 at 9:38 ... Multi-statement Macros in C++ 7/30/2012 3:33:04 AM
|
|
|
|
|
|
|
|
|