A better question about directives

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rcoutts@comcast.net

    A better question about directives

    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

  • Martijn

    #2
    Re: A better question about directives

    > #define foo(f) ((f<3 && f>7) ? 5 : 0)[color=blue]
    >
    > how do you write the directive so that passing a function does result
    > in the function getting performed more than once?[/color]

    #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



    Comment

    • Martijn

      #3
      Re: A better question about directives

      > #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



      Comment

      • rcoutts@comcast.net

        #4
        Re: A better question about directives

        > #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

        Comment

        • Michael Mair

          #5
          Re: A better question about directives

          rcoutts@comcast .net wrote:[color=blue][color=green]
          >>#define foo(f) ( f_temp=(f), (f_temp<3 && f_temp>7)?5:0 )[/color]
          >
          >
          > f_temp is getting flagge as undefined. If I try something like[/color]

          You have to _provide_ f_temp.
          [color=blue]
          > #define foo(f) (int f_temp=(f), (f_temp<3 && f_temp>7)?5:0)
          >
          > the compiler's not liking the "int"[/color]

          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.

          Comment

          • Rouben Rostamian

            #6
            Re: A better question about directives

            In article <1111931117.602 637.47640@l41g2 000cwc.googlegr oups.com>,
            <rcoutts@comcas t.net> wrote:[color=blue]
            >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())[/color]

            Here is one way:

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


            --
            Rouben Rostamian

            Comment

            • Martin Ambuhl

              #7
              Re: A better question about directives

              rcoutts@comcast .net wrote:[color=blue]
              > 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.[/color]

              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.

              Comment

              • Martijn

                #8
                Re: A better question about directives

                rcoutts@comcast .net wrote:[color=blue][color=green]
                >> #define foo(f) ( f_temp=(f), (f_temp<3 && f_temp>7)?5:0 )[/color]
                >
                > f_temp is getting flagge as undefined.[/color]

                oops :)

                --
                Martijn



                Comment

                • Old Wolf

                  #9
                  Re: A better question about directives

                  rcoutts@comcast .net wrote:[color=blue]
                  >
                  > 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?[/color]

                  In this particular example:

                  #define foo(f) 0

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

                  Comment

                  • Keith Thompson

                    #10
                    Re: A better question about directives

                    "Old Wolf" <oldwolf@inspir e.net.nz> writes:[color=blue]
                    > rcoutts@comcast .net wrote:[color=green]
                    >>
                    >> 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?[/color]
                    >
                    > In this particular example:
                    >
                    > #define foo(f) 0
                    >
                    > (it's not possible for f<3 and f>7 to both be true).[/color]

                    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_Keit h) kst-u@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.

                    Comment

                    • CBFalconer

                      #11
                      Re: A better question about directives

                      Old Wolf wrote:[color=blue]
                      > rcoutts@comcast .net wrote:[color=green]
                      >>
                      >> 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?[/color]
                      >
                      > In this particular example:
                      >
                      > #define foo(f) 0
                      >
                      > (it's not possible for f<3 and f>7 to both be true).[/color]

                      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.c om, 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

                      Comment

                      Working...