Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #162378

Re: How to disambiguate macro?

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: How to disambiguate macro?
Date 2021-08-14 13:58 -0700
Organization A noiseless patient Spider
Message-ID <861r6vyc99.fsf@linuxsc.com> (permalink)
References <sd6b8j$498$1@reader1.panix.com> <878s21gney.fsf@bsb.me.uk> <sd6h5j$hng$2@reader1.panix.com> <87fsw9m4qd.fsf@bsb.me.uk> <sd7ud2$hkh$1@reader1.panix.com>

Show all headers | View raw


John Forkosh <forkosh@panix.com> writes:

> Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
>
>> John Forkosh <forkosh@panix.com> writes:
>>
>>> Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
>>>
>>>> John Forkosh <forkosh@panix.com> writes:
>>>>
>>>>> Consider a macro of the form
>>>>> /* --- set north=parent,south=child,east=next,
>>>>>          west=previous link of node --- */
>>>>>    #define setlink(ptr,nsew,link) if ( checknode(ptr) ) \
>>>>>                            ((node *)(ptr))->nsew  = (node *)(link)
>>>>> Not important exactly what it does, just the if(xxx)yyy
>>>>> construction.
>>>>>
>>>>> Now, suppose I want to use it like
>>>>>    if ( xxx ) setlink(a,b,c); else yyy;
>>>>> Then the compiler warns that the binding of that else yyy;  is
>>>>> ambiguous.
>>>>>
>>>>> I clearly mean
>>>>>    if ( xxx ) {setlink(a,b,c);} else yyy;
>>>>> but it's cumbersome and inelegant to write it that way again and again.
>>>>>
>>>>> Likewise, if I try to define the macro with the {}'s (and the
>>>>> trailing ;)
>>>>>    #define setlink(ptr,nsew,link) { if ( checknode(ptr) ) \
>>>>>                            ((node *)(ptr))->nsew  = (node *)(link); }
>>>>> Then writing setlink(a,b,c);  in the code has that extraneous ;
>>>>> which can then again cause the same kind of confusion.  But
>>>>> omitting the ; just looks wrong and ugly.
>>>>>
>>>>> So what's a way to define the macro that simultaneously removes
>>>>> any potential semantic confusion without introducing the
>>>>> necessity of any unpretty syntax?
>>>>
>>>> There is a conventional idiom for this:
>>>>
>>>> #define setlink(ptr,nsew,link)                    \
>>>>   do {                                            \
>>>>      if ( checknode(ptr) )                        \
>>>>          ((node *)(ptr))->nsew  = (node *)(link); \
>>>>   } while (0)
>>>>
>>>> (The {}s are optional in this case.)
>>>
>>> Thanks.  Okay, I'll try adopting that conventional style.
>>> Looks a bit elaborate to me, but if that's the convention
>>> then I guess I can get comfortable with it.
>>
>> Most C programmers won't bat an eyelid at it, but I agree it's
>> clumsy.
>>
>> When the contingent action is actually just an expression, you can
>> avoid the if (...) altogether like this:
>>
>> #define setlink(ptr,nsew,link) \
>>    ( checknode(ptr) && (((node *)(ptr))->nsew = (node *)(link)) )
>>
>> so that there are no 'else' issues at all.
>
> Thanks, again, for the alternative solution.  I actually like it a
> little better since it avoids the klutzy do-while construction that
> has no useful purpose and might only confuse the casual reader,
> i.e., "What the heck is he doing that for?"  But then again, I never
> much liked relying that (a&&b) always evaluates a first, immediately
> becoming 0 without evaluating b at all if a is itself 0.  You never
> know exactly what the next C standard might mess around with.
> But your suggestion itself suggested the more explicit version
>   #define setlink(ptr,nsew,link) \
>      ( checknode(ptr)?  (((node *)(ptr))->nsew = (node *)(link)) : 0 )
> which I think accomplishes the same thing with explicitly visible
> logic.

In cases like this one I usually find an expressional form works
better than a statement form, and ?: better than && (unless of
course the second part of the && is also a predicate and the
overall value is important, in which case it can depend on the
subexpressions involved).

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-20 11:15 +0000
  Re: How to disambiguate macro? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-20 13:49 +0100
    Re: How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-20 12:56 +0000
      Re: How to disambiguate macro? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-20 15:36 +0100
        Re: How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-21 01:48 +0000
          Re: How to disambiguate macro? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-07-20 19:18 -0700
          Re: How to disambiguate macro? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-07-20 20:27 -0700
            Re: How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-21 04:15 +0000
              Re: How to disambiguate macro? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-07-21 02:00 -0700
              Re: How to disambiguate macro? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-07-21 06:02 -0400
          Re: How to disambiguate macro? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-07-21 06:00 -0400
            Re: How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-21 10:10 +0000
              Re: How to disambiguate macro? Bart <bc@freeuk.com> - 2021-07-21 11:30 +0100
              Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-21 13:31 +0200
              Re: How to disambiguate macro? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-21 13:02 +0100
                Re: How to disambiguate macro? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-21 14:20 +0100
                Re: How to disambiguate macro? Bart <bc@freeuk.com> - 2021-07-21 15:15 +0100
                Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-21 16:28 +0200
                Re: How to disambiguate macro? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-21 16:20 +0100
                Re: How to disambiguate macro? Bart <bc@freeuk.com> - 2021-07-21 17:34 +0100
                Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-21 19:46 +0200
                Re: How to disambiguate macro? Manfred <noname@add.invalid> - 2021-07-21 20:42 +0200
                Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-22 08:37 +0200
                Re: How to disambiguate macro? Bart <bc@freeuk.com> - 2021-07-21 20:47 +0100
                Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-22 08:47 +0200
                Re: How to disambiguate macro? Bart <bc@freeuk.com> - 2021-07-22 11:31 +0100
                Re: How to disambiguate macro? antispam@math.uni.wroc.pl - 2021-07-22 14:37 +0000
                Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-22 18:15 +0200
                Re: How to disambiguate macro? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-21 21:19 +0100
                Re: How to disambiguate macro? Manfred <noname@add.invalid> - 2021-07-21 15:42 +0200
                Re: How to disambiguate macro? scott@slp53.sl.home (Scott Lurndal) - 2021-07-21 15:39 +0000
              Re: How to disambiguate macro? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-07-21 09:22 -0700
              Re: How to disambiguate macro? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-07-21 17:24 -0400
                Re: How to disambiguate macro? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-07-21 16:42 -0700
                Re: How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-22 05:49 +0000
                Re: How to disambiguate macro? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-07-22 19:11 -0400
                Re: How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-22 05:30 +0000
                Re: How to disambiguate macro? scott@slp53.sl.home (Scott Lurndal) - 2021-07-22 14:14 +0000
                Re: How to disambiguate macro? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-22 17:06 +0100
                OT: possessive adjectives (Was: How to disambiguate macro?) Manfred <noname@add.invalid> - 2021-07-22 15:04 +0200
                Re: OT: possessive adjectives Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-08-14 13:37 -0700
          Re: How to disambiguate macro? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-08-14 13:58 -0700
  Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-20 14:50 +0200
    Re: How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-20 13:19 +0000
      Re: How to disambiguate macro? Bart <bc@freeuk.com> - 2021-07-20 14:40 +0100
        Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-20 21:02 +0200
      Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-20 21:00 +0200
      Re: How to disambiguate macro? David Brown <david.brown@hesbynett.no> - 2021-07-21 08:15 +0200
      Re: How to disambiguate macro? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-08-14 23:17 -0700
  Re: How to disambiguate macro? John Forkosh <forkosh@panix.com> - 2021-07-20 12:51 +0000
  Re: How to disambiguate macro? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-07-20 08:47 -0700

csiph-web