473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C++ faq 38.5 - macros with multiple statements

This item in the C++ faq:
http://www.parashift.com/c++-faq-lit....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

Jul 22 '05 #1
1 2015
"Erik Haugen" <ha****@Xenon.Stanford.EDU> wrote:
This item in the C++ faq:
http://www.parashift.com/c++-faq-lit....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


Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can...
37
by: michele.simionato | last post by:
Paul Rubin wrote: > How about macros? Some pretty horrible things have been done in C > programs with the C preprocessor. But there's a movememnt afloat to > add hygienic macros to Python. Got any...
25
by: Sabyasachi Basu | last post by:
While trying to port some stuff from Unix to Windows, I encountered a strange behaviour of function macros with empty arguments. Here is a small snippet which illustrates the problem: #include...
11
by: Ben Hetland | last post by:
....in certain cituations they can be useful if not for anything else, then at least for saving a lot of repetetetetetitititive typing. :-) Beyond the point of "do something better instead", I'm...
4
by: DG | last post by:
Hi, Can anyone advise how to execute multiple statements in a single query batch. For example- update customers set customer_name = 'Smith' where customer_name = 'Smyth'; select * from...
24
by: sureshjayaram | last post by:
In some functions where i need to return multiple error codes at multiple places, I use multiple return statements. Say for ex. if (Found == 1) { if (val == -1) return error1; } else { if...
4
by: Chronologic | last post by:
All, I have an issue I would like some expert help on. I understand, or so I believe, that C# does not support the concept of a "compile time macro". At least not in the sense I'm looking...
7
by: aaragon | last post by:
Hi everyone, I have a simple question. I'm trying to make a macro in one file so I can use it in main.cpp. The idea is that I the user of my code has simple to type the macro definition to replace...
20
by: copx | last post by:
Is it true that C macros are unsafe when combined with side effects even if they are "clean" e.g. could foo_macro(++c); execute "++c" multiple times even if foo_macro is "clean"? I have always...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.