Home > Archive > C > February 2006 > Conditional compilation inside a macro
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Conditional compilation inside a macro
|
|
| Srinivas Mudireddy 2006-02-25, 3:55 am |
| Hi,
I am facing a problem with trying to conditionally compile inside a
macro. I have a macro
#define SET_VAL(x, y) ((x) = *(y))
But the problem is y can be NULL in which case I want to set x to 0. So
I modified macro to
#define SET_VAL(x, y)
#if y == NULL
x = 0;
#else
(x) = *(y);
#endif
This is not working since # has a special meaning inside a macro. Is
there any way I can get around this problem with still using a macro? I
can define a function to do the same, but I would prefer to use a
macro.
Thanks,
Srinivas
| |
| Rod Pemberton 2006-02-25, 3:55 am |
|
"Srinivas Mudireddy" <srinivas.mudireddy@gmail.com> wrote in message
news:1140855681.874360.304200@z34g2000cwc.googlegroups.com...
> Hi,
> I am facing a problem with trying to conditionally compile inside a
> macro. I have a macro
>
> #define SET_VAL(x, y) ((x) = *(y))
>
> But the problem is y can be NULL in which case I want to set x to 0. So
> I modified macro to
>
> #define SET_VAL(x, y)
> #if y == NULL
> x = 0;
> #else
> (x) = *(y);
> #endif
>
> This is not working since # has a special meaning inside a macro. Is
> there any way I can get around this problem with still using a macro? I
> can define a function to do the same, but I would prefer to use a
> macro.
Does it fail with line continuation character, '' backslash, at the end of
each line, except the last?
RP
| |
| CBFalconer 2006-02-25, 3:55 am |
| Srinivas Mudireddy wrote:
>
> I am facing a problem with trying to conditionally compile inside
> a macro. I have a macro
>
> #define SET_VAL(x, y) ((x) = *(y))
>
> But the problem is y can be NULL in which case I want to set x to
> 0. So I modified macro to
>
> #define SET_VAL(x, y)
> #if y == NULL
> x = 0;
> #else
> (x) = *(y);
> #endif
>
> This is not working since # has a special meaning inside a macro.
> Is there any way I can get around this problem with still using a
> macro? I can define a function to do the same, but I would prefer
> to use a macro.
#define SET_VAL(x, y) \
do { \
if (y) (x) = *(y); else (x) = 0; \
} while (0)
--
"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
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
| |
| Ian Collins 2006-02-25, 3:55 am |
| Srinivas Mudireddy wrote:
> Hi,
> I am facing a problem with trying to conditionally compile inside a
> macro. I have a macro
>
> #define SET_VAL(x, y) ((x) = *(y))
>
> But the problem is y can be NULL in which case I want to set x to 0. So
> I modified macro to
>
> #define SET_VAL(x, y)
> #if y == NULL
> x = 0;
> #else
> (x) = *(y);
> #endif
>
> This is not working since # has a special meaning inside a macro. Is
> there any way I can get around this problem with still using a macro? I
> can define a function to do the same, but I would prefer to use a
> macro.
>
Why on Earth would you want to do that? Just use an inline function.
--
Ian Collins.
| |
| Ian Collins 2006-02-25, 3:55 am |
| CBFalconer wrote:
> Srinivas Mudireddy wrote:
>
>
>
> #define SET_VAL(x, y) \
> do { \
> if (y) (x) = *(y); else (x) = 0; \
> } while (0)
>
Or
#define SET_VAL(x, y) ((x) = (y) ? *(y) : 0)
But still fragile, try invoking with NULL for y.
--
Ian Collins.
| |
| Ian Collins 2006-02-25, 3:55 am |
| Ian Collins wrote:
> Why on Earth would you want to do that? Just use an inline function.
>
Sorry, I thought I was down the hall in the C++ group where my
pathological hatred of macros belongs...
--
Ian Collins.
| |
| WaterWalk 2006-02-25, 7:55 am |
|
Srinivas Mudireddy =E5=86=99=E9=81=93=EF=BC=9A
> Hi,
> I am facing a problem with trying to conditionally compile inside a
> macro. I have a macro
>
> #define SET_VAL(x, y) ((x) =3D *(y))
>
> But the problem is y can be NULL in which case I want to set x to 0. So
> I modified macro to
>
> #define SET_VAL(x, y)
> #if y =3D=3D NULL
> x =3D 0;
> #else
> (x) =3D *(y);
> #endif
>
> This is not working since # has a special meaning inside a macro. Is
> there any way I can get around this problem with still using a macro? I
> can define a function to do the same, but I would prefer to use a
> macro.
>
> Thanks,
> Srinivas
Maybe this will work:
#define SET_VAL(x, y) ( y =3D=3D NULL ? (x=3D0) : ((x) =3D *(y)))
| |
|
| Ian Collins wrote:
>
> CBFalconer wrote:
>
> Or
> #define SET_VAL(x, y) ((x) = (y) ? *(y) : 0)
>
> But still fragile, try invoking with NULL for y.
It will work if y is a null pointer object,
which I think is probably sufficient,
because I don't think it makes sense to write
SET_VAL(x, NULL) in code,
when you know that zero is the value that you want there.
--
pete
| |
| CBFalconer 2006-02-25, 7:55 am |
| Ian Collins wrote:
> CBFalconer wrote:
>
> Or
> #define SET_VAL(x, y) ((x) = (y) ? *(y) : 0)
>
> But still fragile, try invoking with NULL for y.
I see no difficulty with NULL.
--
"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
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
| |
|
| CBFalconer wrote:
>
> Ian Collins wrote:
>
> I see no difficulty with NULL.
((x) = (NULL) ? *(NULL) : 0)
The compiler won't translate the *(NULL) expression.
But as I implied elsethread, I think it's more likely
that OP was considering that y might be a null pointer lvalue,
than that OP would actually write
SET_VAL(x, NULL);
in his source code.
--
pete
| |
| Serve Laurijssen 2006-02-25, 7:55 am |
|
"Ian Collins" <ian-news@hotmail.com> wrote in message
news:1140860848.731369@drone2-svc-skyt.qsi.net.nz...
> Sorry, I thought I was down the hall in the C++ group where my
> pathological hatred of macros belongs...
C knows inline functions too these days :)
| |
| Jordan Abel 2006-02-25, 6:55 pm |
| On 2006-02-25, Srinivas Mudireddy <srinivas.mudireddy@gmail.com> wrote:
> Hi,
> I am facing a problem with trying to conditionally compile inside a
> macro. I have a macro
>
> #define SET_VAL(x, y) ((x) = *(y))
>
> But the problem is y can be NULL in which case I want to set x to 0. So
> I modified macro to
>
> #define SET_VAL(x, y)
> #if y == NULL
> x = 0;
> #else
> (x) = *(y);
> #endif
>
> This is not working since # has a special meaning inside a macro.
No, it's not working because preprocessor directives can only expand to
one line, and they cannot expand to other preprocessor directives.
> Is
> there any way I can get around this problem with still using a macro? I
> can define a function to do the same, but I would prefer to use a
> macro.
#define SET_VAL(x,y) ((x)=(y)?(*y):0)
| |
| Srinivas Mudireddy 2006-02-25, 6:55 pm |
|
Ian Collins wrote:
> Ian Collins wrote:
Thats true. I can use inline functions. Forgot about that.
[color=darkred]
> Sorry, I thought I was down the hall in the C++ group where my
> pathological hatred of macros belongs...
>
> --
> Ian Collins.
| |
| Srinivas Mudireddy 2006-02-25, 6:55 pm |
|
WaterWalk wrote:
> Srinivas Mudireddy =E5=86=99=E9=81=93=EF=BC=9A
>
>
> Maybe this will work:
> #define SET_VAL(x, y) ( y =3D=3D NULL ? (x=3D0) : ((x) =3D *(y)))
Thx for the suggestion. I thought my simplified example will be enough,
but seems like it is confusing. There are 3 subsystems involved here.
Subsystem A talks about QoS in terms of structure X. Variables x and y
are of type X. Subsystem C tells subsystem B whether QoS is granted or
not in terms of structure M. B looks at M and checks if it has a
special value which means that QoS is not granted. If it is not, it
calls SET_VAL(x, NULL). If QoS is granted, it calls SET_VAL(x, y). When
NULL is passed, I would like to memset x with 0. So ternary operation
will not work.
I apologise for giving a wrong example and throwing you guys in wrong
direction.
Thx,
Srinivas
| |
| Jack Klein 2006-02-25, 6:55 pm |
| On Sat, 25 Feb 2006 03:40:25 -0500, "Rod Pemberton"
<do_not_have@sorry.bitbucket.cmm> wrote in comp.lang.c:
>
> "Srinivas Mudireddy" <srinivas.mudireddy@gmail.com> wrote in message
> news:1140855681.874360.304200@z34g2000cwc.googlegroups.com...
>
> Does it fail with line continuation character, '' backslash, at the end of
> each line, except the last?
If it does not "fail" with the modification you suggested, the
implementation is very severely non-conforming.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~.../FAQ-acllc.html
| |
| obdict 2006-02-26, 6:55 pm |
| Do you have to guard against the scenario that y might be void*?
| |
| CBFalconer 2006-02-26, 6:55 pm |
| obdict wrote:
>
> Do you have to guard against the scenario that y might be void*?
I dunno. It would seem to depend on the usage of y. Without any
context that is totally unknown.
--
"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
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
| |
| Chris Torek 2006-02-27, 3:57 am |
| [regarding defining a macro weirdly]
In article <1140891347.064160.207770@i39g2000cwa.googlegroups.com>
Srinivas Mudireddy <srinivas.mudireddy@gmail.com> wrote:
>Thx for the suggestion. I thought my simplified example will be enough,
>but seems like it is confusing. ...
[In one case, other code the OP probably did not write]
>calls SET_VAL(x, NULL).
[In a completely different case, that other code]
>calls SET_VAL(x, y). When NULL is passed, I would like to memset x
>with 0. So ternary operation will not work.
It can still be made to work using a variant like the one someone
suggested, e.g.:
#define SET_VAL(x, y) \
((y) == NULL ? \
(void)memset(&(x), 0, sizeof(x)) : \
(void)((x) = *(y)))
However, this strikes me as solving the wrong problem, which can
be re-described as follows:
"Some code I did not write insists on using macro X with
particular arguments for situation A, and the same macro X with
different arguments for completely different situation B."
The question was then:
"How can I write a complicated version of macro X that
distinguishes between the two situations based on one of the
arguments?"
when the obvious "right" approach is: "Change the other code to
use two *different* macros for the two different situations."
(Given the text I snipped, possible macros might be something like
"ALLOW_QoS" and "DENY_QoS". The "DENY" macro would take only one
argument, rather than two.)
That is, fix the real problem, rather than kludging up a work-around.
(Of course, sometimes fixing the real problem is rejected; occasionally
it is even rejected for a good reason.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
| |
| Micah Cowan 2006-02-27, 6:57 pm |
| "Rod Pemberton" <do_not_have@sorry.bitbucket.cmm> writes:
> "Srinivas Mudireddy" <srinivas.mudireddy@gmail.com> wrote in message
> news:1140855681.874360.304200@z34g2000cwc.googlegroups.com...
>
> Does it fail with line continuation character, '' backslash, at the end of
> each line, except the last?
It had certainly /better/ fail, regardless of backslash.
|
|
|
|
|