473,835 Members | 1,832 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 #1
12 3065
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?

It enables you to have a macro define a local scope, confine an `if'
statement or both -- and still end it with a semicolon.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #2
Artie Gold 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?

It enables you to have a macro define a local scope, confine an `if'
statement or both -- and still end it with a semicolon.

HTH,
--ag

Why not simpley use '{' and '}'?
#include <iostream>

int main () {
{
if(true) {
std::cout << "I don't get it." << std::endl;
}
};
}

--
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 #3
Steven T. Hatton wrote:
Artie Gold 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?

It enables you to have a macro define a local scope, confine an `if'
statement or both -- and still end it with a semicolon.

HTH,
--ag

Why not simpley use '{' and '}'?
#include <iostream>

int main () {
{
if(true) {
std::cout << "I don't get it." << std::endl;
}
};
}


Try it. Compare the results.

#define LIMITSCOPE(what ) { int what = 42; printf("LIMITIN G " ## #what); }
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc > 1)
LIMITSCOPE(abc) ; // pay attention to this line
else
printf("not more");
}
---------------------------------
#define LIMITSCOPE(what ) do { \
int what = 42; printf("LIMITIN G " ## #what); } while(0)
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc > 1)
LIMITSCOPE(abc) ; // pay attention to this line
else
printf("not more");
}
V
Jul 23 '05 #4
Steven T. Hatton wrote:
Artie Gold 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?

It enables you to have a macro define a local scope, confine an `if'
statement or both -- and still end it with a semicolon.

HTH,
--ag

Why not simpley use '{' and '}'?
#include <iostream>

int main () {
{
if(true) {
std::cout << "I don't get it." << std::endl;
}
};
}


Think what happens if such a macro (without the do/while(0) wrapper) is
used as the `then' clause of an if. [Hint, the extra `;' would then
introduce a null statement.]

--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #5
Artie Gold wrote:
Steven T. Hatton wrote:
Artie Gold 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?

It enables you to have a macro define a local scope, confine an `if'
statement or both -- and still end it with a semicolon.

HTH,
--ag

Why not simpley use '{' and '}'?
#include <iostream>

int main () {
{
if(true) {
std::cout << "I don't get it." << std::endl;
}
};
}


Think what happens if such a macro (without the do/while(0) wrapper) is
used as the `then' clause of an if. [Hint, the extra `;' would then
introduce a null statement.]

--ag


I see from Victor's example what happens. I figured it was something like
that, but until recently, I have systematically avoided omitting the
brackets in if(), for() and while() statements even when the are not
necessary. As you can see, that was the form I chose without considering
whether it was needed or not.

Furthermore, I am of the opinion that Cpp must be destroyed.
- Cato the Elder (Marcus Porcius Cato)[*]
[*]D&E, Chapter 18
--
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 #6
Furthermore, I am of the opinion that Cpp must be destroyed.
- Cato the Elder (Marcus Porcius Cato)[*]


Shouldn't that be "ceterum censeo C++ esse delendam" ?
And the correct translation would be
"Furthermor e I think that C++ is to be destroyed" AFAIK.
Jul 23 '05 #7
Paul Groke wrote:
"Furthermor e I think that C++ is to be destroyed" AFAIK.


I don't think Cpp and C++ are the same thing.
;-)

Jul 23 '05 #8
leonardo77 wrote:
Paul Groke wrote:
"Furthermor e I think that C++ is to be destroyed" AFAIK.

I don't think Cpp and C++ are the same thing.
;-)


Then just search and replace. ;-)
Jul 23 '05 #9
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
Jul 23 '05 #10

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

Similar topics

2
2173
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
4271
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
9484
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
14132
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
1426
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
31994
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
1735
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
71985
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
25544
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
9652
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
10812
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
10523
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...
1
10561
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10235
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9346
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
7766
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
6966
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
4434
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

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.