A better question about directives

R

rcoutts

I muddied my last question with c++ so here it is again:

When writing a directive, if you need to refer to the param a couple of
times, e.g.,

#define foo(f) ((f<3 && f>7) ? 5 : 0)

how do you write the directive so that passing a function does result
in the function getting performed more than once? E.g., when I write

foo(bar())

it's getting performed like

((bar()<3 && bar()>7) ? 5 : 0)

which is a real problem if bar() is performing an operation that should
only be done once, like popping a stack.

Thanks,
Rich
 
M

Martijn

#define foo(f) ((f said:
how do you write the directive so that passing a function does result
in the function getting performed more than once?

#define foo(f) ( f_temp=f, (f_temp<3 && f_temp>7)?5:0 )

Just a thought. But this might be masking an existing f_temp, so you may
need to go for a better name.

Good luck,
 
M

Martijn

#define foo(f) ( f_temp=f, (f_temp<3 && f_temp>7)?5:0 )

Make that:

#define foo(f) ( f_temp=(f), (f_temp<3 && f_temp>7)?5:0 )
 
R

rcoutts

#define foo(f) ( f_temp=(f), (f_temp<3 && f_temp>7)?5:0 )

f_temp is getting flagge as undefined. If I try something like

#define foo(f) (int f_temp=(f), (f_temp<3 && f_temp>7)?5:0)

the compiler's not liking the "int"

Rich
 
M

Michael Mair

f_temp is getting flagge as undefined. If I try something like

You have to _provide_ f_temp.
#define foo(f) (int f_temp=(f), (f_temp<3 && f_temp>7)?5:0)

the compiler's not liking the "int"

No wonder. You are mixing a declaration and the use of the
comma operator.

Try
#define foo(f, f_temp) \
((f_temp)=(f), ((f_temp)<3 && (f_temp)>7)?5:0)
where you have to provide f_temp.

Alternatively, just use a function.


Cheers
Michael
 
R

Rouben Rostamian

I muddied my last question with c++ so here it is again:

When writing a directive, if you need to refer to the param a couple of
times, e.g.,

#define foo(f) ((f<3 && f>7) ? 5 : 0)

how do you write the directive so that passing a function does result
in the function getting performed more than once? E.g., when I write

foo(bar())

Here is one way:

int tmp;
....
tmp = bar();
foo(tmp);
 
M

Martin Ambuhl

I muddied my last question with c++ so here it is again:

When writing a directive, if you need to refer to the param a couple of
times, e.g.,

#define foo(f) ((f<3 && f>7) ? 5 : 0)

how do you write the directive so that passing a function does result
in the function getting performed more than once? E.g., when I write

foo(bar())

it's getting performed like

((bar()<3 && bar()>7) ? 5 : 0)

which is a real problem if bar() is performing an operation that should
only be done once, like popping a stack.

Macros should not be used when possible multiple evaluation is a
problem. Suppose, for the moment, that 'f' is expected to be an int:

inline int foo(int f) { return (f < 3 && f > 7) ? 5 : 0; }

f will be evaluated once.
 
O

Old Wolf

When writing a directive, if you need to refer to the param a
couple of times, e.g.,

#define foo(f) ((f<3 && f>7) ? 5 : 0)

how do you write the directive so that passing a function does
result in the function getting performed more than once?

In this particular example:

#define foo(f) 0

(it's not possible for f<3 and f>7 to both be true).
 
K

Keith Thompson

Old Wolf said:
In this particular example:

#define foo(f) 0

(it's not possible for f<3 and f>7 to both be true).

Allow me to miss the point entirely for just a moment.

The following program:

#include <stdio.h>
#define foo(f) ((f<3 && f>7) ? 5 : 0)
int main(void)
{
int result = foo(2||3);
printf("result = %d\n", result);
return 0;
}

prints "result = 5".

Of course, the macro definition should be:

#define foo(f) (((f)<3 && (f)>7) ? 5 : 0)
 
C

CBFalconer

Old said:
In this particular example:

#define foo(f) 0

(it's not possible for f<3 and f>7 to both be true).

Yes it is :) but you won't like it.

#define f (g += 5)

then the above expands to:

(((g += 4) < 3 && (g += 4) > 7) ? 5 : 0)

which will evalate to 5 for an entry g of -2. <g> Which
illustrates to the OP why you don't want to use macros that require
multiple evaluation of arguments.
 

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,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top