By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,911 Members | 1,171 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,911 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
12 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.