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

assert-macro: brackets

P: n/a
Leo
Hi @ all.

I looked up the implementation of the assert macro of my compiler
(MinGW), because I wanna write my own assert.

I found this:

#define assert(x) ((void)0)
#define assert(e) ((e) ? (void)0 : _assert(#e, __FILE__, __LINE__))

My question is: Do the outer brackets have any reason? If yes, which?

Is this not equivalent to the above code?:

#define assert(x) (void)0
#define assert(e) (e) ? (void)0 : _assert(#e, __FILE__, __LINE__)
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Sat, 24 Jul 2004 22:39:45 +0200, Leo <le*******@email.com> wrote:
Hi @ all.

I looked up the implementation of the assert macro of my compiler
(MinGW), because I wanna write my own assert.

I found this:

#define assert(x) ((void)0)
#define assert(e) ((e) ? (void)0 : _assert(#e, __FILE__, __LINE__))

My question is: Do the outer brackets have any reason? If yes, which?

Is this not equivalent to the above code?:

#define assert(x) (void)0
#define assert(e) (e) ? (void)0 : _assert(#e, __FILE__, __LINE__)


No, try the following with your two versions of assert

if (0 && assert(0))
{
}

With the first definition this will not call _assert (which is correct),
with the second definition it will.

john
Jul 22 '05 #2

P: n/a
John Harrison wrote:
On Sat, 24 Jul 2004 22:39:45 +0200, Leo <le*******@email.com> wrote:
....
No, try the following with your two versions of assert

if (0 && assert(0))
I think you meant

if (0 && assert(1))
{
}

With the first definition this will not call _assert (which is
correct), with the second definition it will.


G
Jul 22 '05 #3

P: n/a
Leo
John Harrison wrote:
On Sat, 24 Jul 2004 22:39:45 +0200, Leo <le*******@email.com> wrote:
Hi @ all.

I looked up the implementation of the assert macro of my compiler
(MinGW), because I wanna write my own assert.

I found this:

#define assert(x) ((void)0)
#define assert(e) ((e) ? (void)0 : _assert(#e, __FILE__, __LINE__))

My question is: Do the outer brackets have any reason? If yes, which?

Is this not equivalent to the above code?:

#define assert(x) (void)0
#define assert(e) (e) ? (void)0 : _assert(#e, __FILE__, __LINE__)

No, try the following with your two versions of assert

if (0 && assert(0))
{
}

With the first definition this will not call _assert (which is
correct), with the second definition it will.

john


Both versions won't compile :D
Jul 22 '05 #4

P: n/a
On Sun, 25 Jul 2004 00:38:29 +0200, Leo <le*******@email.com> wrote:
John Harrison wrote:
On Sat, 24 Jul 2004 22:39:45 +0200, Leo <le*******@email.com> wrote:
Hi @ all.

I looked up the implementation of the assert macro of my compiler
(MinGW), because I wanna write my own assert.

I found this:

#define assert(x) ((void)0)
#define assert(e) ((e) ? (void)0 : _assert(#e, __FILE__,
__LINE__))

My question is: Do the outer brackets have any reason? If yes, which?

Is this not equivalent to the above code?:

#define assert(x) (void)0
#define assert(e) (e) ? (void)0 : _assert(#e, __FILE__, __LINE__)

No, try the following with your two versions of assert
if (0 && assert(0))
{
}
With the first definition this will not call _assert (which is
correct), with the second definition it will.
john


Both versions won't compile :D


Yes, I realised that after I posted.

How about this?

1 && assert(0);

That fails to compile with version 1, but compiles with version 2.

I'm sure a better example could be devised, but the point is that the
extra bracket in the first version avoids any surprises.

john
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.