assert-macro: brackets | | |
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__) | | | | 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 | | | | 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 | | | | 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 | | | | 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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,531 network members.
|