Need for the ternary operator

G

glongword

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?
 
L

Leor Zolman

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
 
C

Charles Banas

Leor said:
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.
 
G

glongword

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

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.
 
L

Leor Zolman

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
 
E

Eric Amick

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.
 
D

Dan Pop

In said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top