assert-macro: brackets

Discussion in 'C++' started by Leo, Jul 24, 2004.

  1. Leo

    Leo Guest

    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__)
    Leo, Jul 24, 2004
    #1
    1. Advertising

  2. On Sat, 24 Jul 2004 22:39:45 +0200, Leo <> 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
    John Harrison, Jul 24, 2004
    #2
    1. Advertising

  3. John Harrison wrote:
    > On Sat, 24 Jul 2004 22:39:45 +0200, Leo <> 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
    Gianni Mariani, Jul 24, 2004
    #3
  4. Leo

    Leo Guest

    John Harrison wrote:
    > On Sat, 24 Jul 2004 22:39:45 +0200, Leo <> 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
    Leo, Jul 24, 2004
    #4
  5. On Sun, 25 Jul 2004 00:38:29 +0200, Leo <> wrote:

    > John Harrison wrote:
    >> On Sat, 24 Jul 2004 22:39:45 +0200, Leo <> 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
    John Harrison, Jul 25, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephen Tyndall

    Custom ASSERT macro

    Stephen Tyndall, Jul 16, 2004, in forum: C++
    Replies:
    7
    Views:
    3,756
    Stephen Tyndall
    Jul 16, 2004
  2. Robert Brewer
    Replies:
    1
    Views:
    487
    bsmith
    Nov 7, 2004
  3. Thomas Guettler

    assert 0, "foo" vs. assert(0, "foo")

    Thomas Guettler, Feb 23, 2005, in forum: Python
    Replies:
    3
    Views:
    2,519
    Carl Banks
    Feb 23, 2005
  4. Alex Vinokur

    assert(x) and '#define ASSERT(x) assert(x)'

    Alex Vinokur, Nov 25, 2004, in forum: C Programming
    Replies:
    5
    Views:
    913
    Keith Thompson
    Nov 25, 2004
  5. ImpalerCore

    To assert or not to assert...

    ImpalerCore, Apr 27, 2010, in forum: C Programming
    Replies:
    79
    Views:
    1,659
    Richard Bos
    May 17, 2010
Loading...

Share This Page