Ok, is *this* legal?

A

Ancient_Hacker

Okay, now we know the function name can be conditionalized, Can a
parameter list go into a conditional? Something like:

int Func( float Angle, char * WhichMode ) { .. }


Result = Func Mode == Debug ? ( 3.14159, "Debugging" ) : ( Pi,
"Release" )

Now THAT really looks weird.
 
S

Scorpio

Ancient_Hacker said:
Okay, now we know the function name can be conditionalized, Can a
parameter list go into a conditional?
No.

Something like:

int Func( float Angle, char * WhichMode ) { .. }


Result = Func Mode == Debug ? ( 3.14159, "Debugging" ) : ( Pi,
"Release" )

Now THAT really looks weird.

Yes, and it doesn't work.

Find out what your conditional statment would return.
It doesn't return a list of values. Just one value.
 
F

Frederick Gotham

Ancient_Hacker posted:
Okay, now we know the function name can be conditionalized,


Confusing use of the verb "conditionalise".

Can a parameter list go into a conditional? Something like:

int Func( float Angle, char * WhichMode ) { .. }


Result = Func Mode == Debug ? ( 3.14159, "Debugging" ) : ( Pi,
"Release" )

Now THAT really looks weird.


Have you got a C compiler? If so, then why are you asking _us_?
 
N

Nelu

Frederick said:
Ancient_Hacker posted:



Confusing use of the verb "conditionalise".




Have you got a C compiler? If so, then why are you asking _us_?

Because the compiler doesn't necessarily give an answer to the
question "is this allowed by the standard?".
 
K

Keith Thompson

Ancient_Hacker said:
Okay, now we know the function name can be conditionalized, Can a
parameter list go into a conditional? Something like:

int Func( float Angle, char * WhichMode ) { .. }


Result = Func Mode == Debug ? ( 3.14159, "Debugging" ) : ( Pi,
"Release" )

No, because an argument list is not an expression.
 
B

Ben C

Okay, now we know the function name can be conditionalized, Can a
parameter list go into a conditional? Something like:

int Func( float Angle, char * WhichMode ) { .. }


Result = Func Mode == Debug ? ( 3.14159, "Debugging" ) : ( Pi,
"Release" )

Now THAT really looks weird.

I see what you're thinking, but this would only work if ?: were
interpreted by the preprocessor rather than by the compiler.

You can do this:

Result = Func(Mode == Debug ? 3.14 : Pi,
Mode == Debug ? "Debugging" : "Release");

or if Mode is known at compile time:

#if DEBUG
# define ARGS 3.14, "Debugging"
#else
# define ARGS Pi, "Release"
#endif

func(ARGS);
 
R

Richard Tobin

Ancient_Hacker said:
Result = Func Mode == Debug ? ( 3.14159, "Debugging" ) : ( Pi,
"Release" )

Read the grammar. A use of the conditional operator is an expression.
In the production for a function call, is the parenthesized argument
list an expression?

-- Richard
 
F

Frederick Gotham

Nelu posted:
Because the compiler doesn't necessarily give an answer to the
question "is this allowed by the standard?".


I'd be happier if the post went something like:


I tried to compile it on my C89-conforming compiler, and it failed. Could
someone please confirm that this shouldn't compile?
 
N

Nelu

Frederick said:
Nelu posted:



I'd be happier if the post went something like:


I tried to compile it on my C89-conforming compiler, and it failed. Could
someone please confirm that this shouldn't compile?

The problem was not "not compiling" but if it compiled was it
because the standard says so or because my compiler has some
really strange extensions that I don't know about? :).
 
M

Michael Wojcik

I see what you're thinking, but this would only work if ?: were
interpreted by the preprocessor rather than by the compiler.

It'd work just fine at runtime if C made argument list a first-class
type. But it does not.

There are languages - usually functional ones - where you can
construct arbitrary argument lists at runtime and apply them to
functions. You can do this sort of thing easily in an ML variant
such as SML or OCaml, for example.

--
Michael Wojcik (e-mail address removed)

Some there are, brave, high-souled fellows, who could borrow the world to
play at ball, and never feel the responsibility, whereas others are uneasy
and not themselves with a single shilling that does not belong to them.
-- Arthur Ransome
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top