Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Need for the ternary operator

Reply
Thread Tools

Need for the ternary operator

 
 
glongword
Guest
Posts: n/a
 
      05-15-2004
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?
 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      05-16-2004
On 15 May 2004 16:14:56 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
 
Charles Banas
Guest
Posts: n/a
 
      05-16-2004
Leor Zolman wrote:
> On 15 May 2004 16:14:56 -0700, (E-Mail Removed) (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
 
Reply With Quote
 
glongword
Guest
Posts: n/a
 
      05-16-2004
Charles Banas <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> > 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.
 
Reply With Quote
 
Leor Zolman
Guest
Posts: n/a
 
      05-16-2004
On Sat, 15 May 2004 21:02:30 -0600, Charles Banas <(E-Mail Removed)> wrote:

>Leor Zolman wrote:
>> On 15 May 2004 16:14:56 -0700, (E-Mail Removed) (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
 
Reply With Quote
 
Eric Amick
Guest
Posts: n/a
 
      05-16-2004
On 15 May 2004 16:14:56 -0700, (E-Mail Removed) (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
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      05-17-2004
In <(E-Mail Removed) > (E-Mail Removed) (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: (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function call selection using ternary operator marco_segurini C++ 4 09-21-2004 01:37 PM
ternary operator and ostreams Roger Leigh C++ 6 01-19-2004 07:02 PM
Union, ternary operator, Macro, printf don't cooperate for me. Help? Paul E Johnson C Programming 2 10-17-2003 06:41 AM
union, ternary operator, and C. What a mess! Paul E Johnson C Programming 3 10-17-2003 03:44 AM
ternary operator error Jacob Java 12 07-02-2003 07:12 PM



Advertisments