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 10 1055
> #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?
#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,
--
Martijn http://www.sereneconcepts.nl
> #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 )
--
Martijn http://www.sereneconcepts.nl
> #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 rc*****@comcast.net wrote: #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
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
In article <11*********************@l41g2000cwc.googlegroups. com>,
<rc*****@comcast.net> wrote: 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);
--
Rouben Rostamian rc*****@comcast.net wrote: 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. rc*****@comcast.net wrote: 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).
"Old Wolf" <ol*****@inspire.net.nz> writes: rc*****@comcast.net wrote: 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).
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)
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Old Wolf wrote: rc*****@comcast.net wrote: 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).
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.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Edward K. Ream |
last post by:
Hello all,
First of all, my present state of mind re pep 318 is one of sheepish
confusion. I suspect pep 318 will not affect Leo significantly, but I am
most surprised that an apparently...
|
by: Gene Ellis |
last post by:
Something strange is happening, but I bet it is a quick fix. My
favicon image is showing up in the URL bar, for a couple of seconds,
but then it disappears and the default browser icon is...
|
by: seemanta dutta |
last post by:
Greetings C gurus,
I have used preprocessor directives since a very long time. But
whenever I see some professional piece of C code, the linux kernel for
example, I get literally confused by the...
|
by: Xiangliang Meng |
last post by:
Hi, all.
Recently, I find there is a way in our project to maintain a global set in
many files by using preprocessing directives. I'm wondering if we could find
a better method for this.
Many...
|
by: wanghz |
last post by:
Hi,
Could I ask some questions about the conditional compilaion? Suppose I
have three simple files: a.c, b.c and c.h
/* --------a.c--------- */
#include <stdio.h>
#include "c.h"
int...
|
by: cody |
last post by:
the following leads to an error (note that TEST is not defined):
#if TEST
string s = @"
#"; // <-- the error is "preprocessing directive expected"
#endif
also, here we get the same error:
...
|
by: rincewind |
last post by:
I remember reading an article (was it Herb Sutter's?) that recommended
avoiding using directives. While I quite understand this recommendation
for headers, what's wrong in using directive in .cpp...
|
by: bcr07548 |
last post by:
I am writing a C++ wrapper class for a library that was available in C
but am having some trouble because, at some point, one function was
replaced by a similar one. I would like to have it so my...
|
by: lagman |
last post by:
All,
I'm looking for a tool that is able to take my code base (which is
full of preprocessor directives) and spit out source code that is
"clean" of any preprocessor directives based on the...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |