Need for the ternary operator

Discussion in 'C Programming' started by glongword, May 16, 2004.

  1. glongword

    glongword Guest

    As the assert macro should evaluate to a void expression, it should
    not have an 'if statement' in its definition. Does this necessitate
    the existence of a ternary conditional operator? Is there a way
    assert.h can be implemented without using it? Are these decisions
    related in anyway?
     
    glongword, May 16, 2004
    #1
    1. Advertising

  2. glongword

    Leor Zolman Guest

    On 15 May 2004 16:14:56 -0700, (glongword) wrote:

    >As the assert macro should evaluate to a void expression, it should
    >not have an 'if statement' in its definition. Does this necessitate
    >the existence of a ternary conditional operator? Is there a way
    >assert.h can be implemented without using it? Are these decisions
    >related in anyway?


    Here's how the assert macro is defined in the VC6 library (the first and
    only one I looked at):

    #define assert(exp) (void)( (exp) || (_assert(#exp, __FILE__, __LINE__), 0)
    )


    I don't see any ternary operators there. So I guess assert can be
    implemented without one. I don't understand your final question, but if
    you're asking whether the ternary operator is in the language specifically
    to support assert macros, then I seriously doubt it.
    -leor


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, May 16, 2004
    #2
    1. Advertising

  3. Leor Zolman wrote:
    > On 15 May 2004 16:14:56 -0700, (glongword) wrote:
    >
    >
    >>As the assert macro should evaluate to a void expression, it should
    >>not have an 'if statement' in its definition. Does this necessitate
    >>the existence of a ternary conditional operator? Is there a way
    >>assert.h can be implemented without using it? Are these decisions
    >>related in anyway?

    >
    >
    > Here's how the assert macro is defined in the VC6 library (the first and
    > only one I looked at):
    >

    VC6 isn't the only library. also note that it makes reference to _assert()

    > #define assert(exp) (void)( (exp) || (_assert(#exp, __FILE__, __LINE__), 0)
    > )
    >

    here is the contents of Cygwin's assert.h:

    /*
    assert.h
    */

    #ifdef __cplusplus
    extern "C" {
    #endif

    #include "_ansi.h"

    #undef assert

    #ifdef NDEBUG /* required by ANSI standard */
    #define assert(p) ((void)0)
    #else

    #ifdef __STDC__
    #define assert(e) ((e) ? (void)0 : __assert(__FILE__, __LINE__, #e))
    #else /* PCC */
    #define assert(e) ((e) ? (void)0 : __assert(__FILE__, __LINE__, "e"))
    #endif

    #endif /* NDEBUG */

    void _EXFUN(__assert,(const char *, int, const char *));

    #ifdef __cplusplus
    }
    #endif

    it very much uses the ternary operator.

    the copy on my Gentoo box is similar, but more extensive. it also uses
    the ternary operator.

    >
    > I don't see any ternary operators there. So I guess assert can be
    > implemented without one.
    >

    you might want to check the definition of _assert() before you make that
    statement.

    > I don't understand your final question, but if
    > you're asking whether the ternary operator is in the language specifically
    > to support assert macros, then I seriously doubt it.
    > -leor
    >
    >



    --
    -- Charles Banas
     
    Charles Banas, May 16, 2004
    #3
  4. glongword

    glongword Guest

    Charles Banas <> wrote in message news:<>...
    > > I don't see any ternary operators there. So I guess assert can be
    > > implemented without one.
    > >

    > you might want to check the definition of _assert() before you make that
    > statement.
    >
    > > I don't understand your final question, but if
    > > you're asking whether the ternary operator is in the language specifically
    > > to support assert macros, then I seriously doubt it.
    > > -leor


    Right, I think that assert needs the ternary operator because it
    needs to make a decision and it cannot use the if statement. The
    implementation of assert itself might not be done in C, though. So, we
    might not need the ternary operator after all. I wish to clarify if
    the ternary operator is just a convenience or if it is needed to
    support the standard.
     
    glongword, May 16, 2004
    #4
  5. glongword

    Leor Zolman Guest

    On Sat, 15 May 2004 21:02:30 -0600, Charles Banas <> wrote:

    >Leor Zolman wrote:
    >> On 15 May 2004 16:14:56 -0700, (glongword) wrote:
    >>
    >>
    >>>As the assert macro should evaluate to a void expression, it should
    >>>not have an 'if statement' in its definition. Does this necessitate
    >>>the existence of a ternary conditional operator? Is there a way
    >>>assert.h can be implemented without using it? Are these decisions
    >>>related in anyway?

    >>
    >>
    >> Here's how the assert macro is defined in the VC6 library (the first and
    >> only one I looked at):
    >>

    >VC6 isn't the only library. also note that it makes reference to _assert()


    I never said it was "the only library", and I'm not sure why you feel the
    need to clarify that for me.

    I /did/ notice that the assert macro makes reference to _assert(). I don't
    have the source to that, or actually I don't know whether or not I have
    the source, and I don't particularly care. The fact it is a function call
    means that I'm fairly confident that, even if it /did/ use the ternary
    operator somewhere within it, it could trivially be re-written not to. And,
    of course, as the OP has pointed out, _assert need not necessarily even be
    written in C, but I didn't feel the need to get that desperate in looking
    for a way out of requiring the existence of the ternary operator.
    -leor


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
     
    Leor Zolman, May 16, 2004
    #5
  6. glongword

    Eric Amick Guest

    On 15 May 2004 16:14:56 -0700, (glongword) wrote:

    >As the assert macro should evaluate to a void expression, it should
    >not have an 'if statement' in its definition. Does this necessitate
    >the existence of a ternary conditional operator? Is there a way
    >assert.h can be implemented without using it? Are these decisions
    >related in anyway?


    The ternary operator was in C from the beginning (or near enough to it),
    so the assert macro has nothing to do with its presence. If anything, it
    wouldn't surprise me to find the existence of the ternary operator
    influenced the design of the assert macro.

    --
    Eric Amick
    Columbia, MD
     
    Eric Amick, May 16, 2004
    #6
  7. glongword

    Dan Pop Guest

    In <> (glongword) writes:

    >As the assert macro should evaluate to a void expression, it should
    >not have an 'if statement' in its definition. Does this necessitate
    >the existence of a ternary conditional operator? Is there a way
    >assert.h can be implemented without using it?


    Although not idiomatic in C (but relatively popular in Perl and Unix shell
    scripting), the && and || operators can replace if statements by ordinary
    expression statements, due to their shortcircuiting effect.

    flag && i++; is the equivalent of if (flag) i++;
    flag || i++; if the equivalent of if (!flag) i++;

    >Are these decisions related in anyway?


    The conditional operator (there is only one in C) predates the assert()
    macro by a long shot.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, May 17, 2004
    #7
    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. Jacob

    ternary operator error

    Jacob, Jun 30, 2003, in forum: Java
    Replies:
    12
    Views:
    4,752
    pete kirkham
    Jul 2, 2003
  2. Roger Leigh

    ternary operator and ostreams

    Roger Leigh, Jan 16, 2004, in forum: C++
    Replies:
    6
    Views:
    670
    Roger Leigh
    Jan 19, 2004
  3. marco_segurini
    Replies:
    4
    Views:
    814
    Dan Cernat
    Sep 21, 2004
  4. Paul E Johnson

    union, ternary operator, and C. What a mess!

    Paul E Johnson, Oct 17, 2003, in forum: C Programming
    Replies:
    3
    Views:
    472
    Ed Morton
    Oct 17, 2003
  5. Paul E Johnson
    Replies:
    2
    Views:
    584
    Christian Bau
    Oct 17, 2003
Loading...

Share This Page