Connecting Tech Pros Worldwide Forums | Help | Site Map

assert-macro: brackets

Leo
Guest
 
Posts: n/a
#1: Jul 22 '05
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__)

John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

re: assert-macro: brackets


On Sat, 24 Jul 2004 22:39:45 +0200, Leo <leo.klamm@email.com> wrote:
[color=blue]
> 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__)[/color]

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
Gianni Mariani
Guest
 
Posts: n/a
#3: Jul 22 '05

re: assert-macro: brackets


John Harrison wrote:[color=blue]
> On Sat, 24 Jul 2004 22:39:45 +0200, Leo <leo.klamm@email.com> wrote:
>[/color]
....[color=blue]
>
> No, try the following with your two versions of assert
>
> if (0 && assert(0))[/color]

I think you meant

if (0 && assert(1))
[color=blue]
> {
> }
>
> With the first definition this will not call _assert (which is
> correct), with the second definition it will.[/color]

G
Leo
Guest
 
Posts: n/a
#4: Jul 22 '05

re: assert-macro: brackets


John Harrison wrote:[color=blue]
> On Sat, 24 Jul 2004 22:39:45 +0200, Leo <leo.klamm@email.com> wrote:
>[color=green]
>> 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__)[/color]
>
>
> 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[/color]

Both versions won't compile :D
John Harrison
Guest
 
Posts: n/a
#5: Jul 22 '05

re: assert-macro: brackets


On Sun, 25 Jul 2004 00:38:29 +0200, Leo <leo.klamm@email.com> wrote:
[color=blue]
> John Harrison wrote:[color=green]
>> On Sat, 24 Jul 2004 22:39:45 +0200, Leo <leo.klamm@email.com> wrote:
>>[color=darkred]
>>> 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__)[/color]
>> 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[/color]
>
> Both versions won't compile :D[/color]

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
Closed Thread