473,969 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why do{}while(0) in #MACROS?

I've noticed in different places where people who write #MACROS use a null
do/while. For example:

do { POINTER = new CONSTRUCTOR; \
if (POINTER == 0) { errno = ENOMEM; ACE_THROW_INT (EXCEPTION); } \
} while (0)

What does that accomplish?

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05
12 3080
Mike Smith wrote:
Steven T. Hatton wrote:
I've noticed in different places where people who write #MACROS use a
null
do/while. For example:

do { POINTER = new CONSTRUCTOR; \
if (POINTER == 0) { errno = ENOMEM; ACE_THROW_INT (EXCEPTION); } \
} while (0)

What does that accomplish?


You can break out of it if you want, which you cannot do with a simple
local scope.

--
Mike Smith

I'm failing to see the usefulness of that feature. Can you provide an
example?
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #11
Mike Smith wrote:
Steven T. Hatton wrote:
I've noticed in different places where people who write #MACROS use a
null
do/while. For example:

do { POINTER = new CONSTRUCTOR; \
if (POINTER == 0) { errno = ENOMEM; ACE_THROW_INT (EXCEPTION); } \
} while (0)

What does that accomplish?


This to to allow a block/multiline macro that is safe to use in an
IF-ELSE construct.

#define MACRO() do { /* something */; } while (0)

///

if (condition)
MACRO();
else
{
// do something else
}

now, consider the above code with the following definition

#define MACRO() { /* something */ ; }

Now how would it expand out?

The do { } while (0) construct provides a block that must be terminated
by a semicolon.
Jul 23 '05 #12
red floyd wrote:
Mike Smith wrote:
Steven T. Hatton wrote:
I've noticed in different places where people who write #MACROS use a
null
do/while. For example:

do { POINTER = new CONSTRUCTOR; \
if (POINTER == 0) { errno = ENOMEM; ACE_THROW_INT (EXCEPTION); }
\
} while (0)

What does that accomplish?


This to to allow a block/multiline macro that is safe to use in an
IF-ELSE construct.

#define MACRO() do { /* something */; } while (0)

///

if (condition)
MACRO();
else
{
// do something else
}

now, consider the above code with the following definition

#define MACRO() { /* something */ ; }

Now how would it expand out?

The do { } while (0) construct provides a block that must be terminated
by a semicolon.


Yeah, I was a bit slow on the uptake, but Victor and Artie cleared it up for
me. I'm specifically curious about the "ability to break out" comment from
Mike Smith, which I still don't understand.
BTW, I hope people don't think my dislike and objection to the CPP is
unqualified. I /do/ understand where it is useful, and why people like it.
It does "cookiecutt er" work, among other things. The two most useful
functions I've seen for the CPP are selfreferential source extraction, and
conditional compilation.

WTH does "selfreferentia l source extraction" mean? It means grabbing some
text from the source code to be included in the compiled program. E.g.,
class names, function names, variable names etc.

I don't believe I need to define conditional compilation. I will, however,
say it is a rather ugly feature of C++ in so much as it often breaks up the
flow of a program into difficult to follow slices. I wonder if an internal
language construct such as compile_if(bool condition){} might prove
superior.

I'm gonna stick this in the thread about the study of the CPP, but I figure
it may be worth including here:

http://etheses.uwaterloo.ca/display.cfm?ethesis_id=390

I've only skimmed it.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #13

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

Similar topics

2
2183
by: Marco | last post by:
We can read this comment at php.net >jon >05-Nov-2003 10:06 >According to my tests, the "do...while" control structure actually seems to be ~40% faster than the "for" control structure. At least using PHP v4.0.6 >on a few fairly modern Linux machines. >I used a the getmicrotime function as defined at http://www.php.net/microtime
11
4285
by: Angus Graham | last post by:
Hi: I've recently come across a number of programmers who use the following type of do/while(false) structure quite often: do { if (! CatchFish(pFish)) { break; } if (! CleanFish(pFish)) {
3
9491
by: RobG | last post by:
A little while ago I opined that do/while loops are harder to read than for loops, and therefore I preferred using for loops. However, it was pointed out that do/while has significant performance benefits as evidenced by: <URL:http://www.websiteoptimization.com/speed/10/10-2.html> (There's a bug in the page, testLoop is both a function name and the name of the form but if you download the page & rename
36
14154
by: Martin | last post by:
Can anyone help with a quick query... I've seen the ASSERT macro defined as: #define ASSERT(f) \ do { \ if (!(f) && assertFailedOnLine (THIS_FILE, __LINE__)) \ FatalExit (0); \ } while (0) \
4
1433
by: Mohanasundaram | last post by:
Hi All, I saw in some code do{//for error jump /*Some code here*/ }while(0); Can anyone plese tell me what is the purpose of this code? There was a comment saying that //for error jump. What does it mean? There is no
27
32051
by: Yan | last post by:
A lot of times when reading open software, i come across macros that are defined as follows: #define CALL_FUNCS(x) \ do { \ func1(x); \ func2(x); \ func3(x); \ } while (0);
20
1757
by: sathyashrayan | last post by:
What is the use of the above do-while. Is it simply a style issue. Since the above same condition will applied with out a do-while(0); because the loop executes only once. I went through the faq section 10.4 but I am little understood. Any answers please? -- "combination is the heart of chess" A.Alekhine Mail to:
6
72008
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
11
25565
by: Rene | last post by:
Quick question, what is the point for forcing the semicolon at the end of the while statement? See example below: x = 0; do { x = x + 1; }while (x < 3); What's the point of having the semicolon after the (x < 3)? Why can't the
0
10149
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11802
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11394
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10056
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8447
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
5136
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3740
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.