473,394 Members | 1,706 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,394 software developers and data experts.

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 3025
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("LIMITING " ## #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("LIMITING " ## #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
"Furthermore I think that C++ is to be destroyed" AFAIK.
Jul 23 '05 #7
Paul Groke wrote:
"Furthermore 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:
"Furthermore 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
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 "cookiecutter" work, among other things. The two most useful
functions I've seen for the CPP are selfreferential source extraction, and
conditional compilation.

WTH does "selfreferential 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
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...
11
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
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...
36
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...
4
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....
27
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
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...
6
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
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...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.