Can I write a recursive type definition ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Timothy Madden

    #1

    Can I write a recursive type definition ?

    [Cross-posted to: comp.lang.c, comp.lang.c++]

    Hello all

    I'm facing a very strange problem. I need to define a function that takes as
    argument something like a pointer to itself. It takes as argument a pointer
    of the same type as a pointer to itself and with possibly other
    pointer-to-function value. But I don't have a data type for such a pointer
    because the function prototype is in the same time being defined. I hope you
    are following me ...

    Actualy I need to define a pointer to a function. Different modules of my
    app will install a new function to be pointed by this pointer, and each new
    function will remember the address of and call the previous function,
    forming a chain that exists only in code. I guess the idea is used for
    at_exit() function in the library or something like it.

    Now I want the function to take as argument a pointer to another instance of
    the same function type. This will allow for removing functions from the
    chain even in a different order than the one functions were installed, by
    having each installed function recognize it's own address in the argument
    and then return as a result the previous installed function that it knows of

    I hope you are following me...

    So how could I declare the prototype for a function that takes as argument
    and that returns a pointer to the same function type being declared ?

    Thank you
    Timothy Madden
    Romania
    --------------------------------------------------
    And I don't wanna miss a thing


  • Xenos

    #2
    Re: Can I write a recursive type definition ?


    "Timothy Madden" <batman@rmv.spa m.home.ro> wrote in message
    news:2s2jbtF1gb 3abU1@uni-berlin.de...[color=blue]
    > [Cross-posted to: comp.lang.c, comp.lang.c++]
    >
    > Hello all
    >
    > I'm facing a very strange problem. I need to define a function that takes[/color]
    as[color=blue]
    > argument something like a pointer to itself. It takes as argument a[/color]
    pointer[color=blue]
    > of the same type as a pointer to itself and with possibly other
    > pointer-to-function value. But I don't have a data type for such a pointer
    > because the function prototype is in the same time being defined. I hope[/color]
    you[color=blue]
    > are following me ...
    >
    > Actualy I need to define a pointer to a function. Different modules of my
    > app will install a new function to be pointed by this pointer, and each[/color]
    new[color=blue]
    > function will remember the address of and call the previous function,
    > forming a chain that exists only in code. I guess the idea is used for
    > at_exit() function in the library or something like it.
    >
    > Now I want the function to take as argument a pointer to another instance[/color]
    of[color=blue]
    > the same function type. This will allow for removing functions from the
    > chain even in a different order than the one functions were installed, by
    > having each installed function recognize it's own address in the argument
    > and then return as a result the previous installed function that it knows[/color]
    of[color=blue]
    >
    > I hope you are following me...
    >
    > So how could I declare the prototype for a function that takes as argument
    > and that returns a pointer to the same function type being declared ?
    >
    > Thank you
    > Timothy Madden
    > Romania
    > --------------------------------------------------
    > And I don't wanna miss a thing
    >
    >[/color]

    I believe that it tells you how to handle this in the FAQ.


    Regards.


    Comment

    • puppet_sock@hotmail.com

      #3
      Re: Can I write a recursive type definition ?

      "Timothy Madden" <batman@rmv.spa m.home.ro> wrote in message news:<2s2jbtF1g b3abU1@uni-berlin.de>...
      [snip][color=blue]
      > But I don't have a data type for such a pointer
      > because the function prototype is in the same time being defined.[/color]

      You want a function that takes as argument a pointer to a function,
      which takes as argument a pointer to a function, which takes as
      argument a pointer to a function, which ...

      I'm not sure you can do it. I'm not sure you can't do it. But
      it's making me dizzy.

      I think you'd probably be better off if you defined a functional
      member of a class and then had a data member that pointed to the
      appropriate other instance of the class.
      Socks

      Comment

      • Siemel Naran

        #4
        Re: Can I write a recursive type definition ?

        "Timothy Madden" <batman@rmv.spa m.home.ro> wrote in message
        [color=blue]
        > I'm facing a very strange problem. I need to define a function that takes[/color]
        as[color=blue]
        > argument something like a pointer to itself. It takes as argument a[/color]
        pointer[color=blue]
        > of the same type as a pointer to itself and with possibly other
        > pointer-to-function value. But I don't have a data type for such a pointer
        > because the function prototype is in the same time being defined. I hope[/color]
        you[color=blue]
        > are following me ...[/color]

        Could you have an array/vector/stack of function pointers or objects or
        pointers to an abstrat class object? When you want to execute the array,
        you step through each element and execute each one. So rather than
        "different modules of my app will install a new function to be pointed by
        this pointer, and each new function will remember the address of and call
        the previous function", you'll insert new function pointers or objects or
        pointers to objects into the array.

        If you literally want to call a function that returns a function, I think
        you can setup a class hierarchy and call a class member function that
        returns a new class, and this new class stores a pointer to the original
        class.
        [color=blue]
        > Actualy I need to define a pointer to a function. Different modules of my
        > app will install a new function to be pointed by this pointer, and each[/color]
        new[color=blue]
        > function will remember the address of and call the previous function,
        > forming a chain that exists only in code. I guess the idea is used for
        > at_exit() function in the library or something like it.
        >
        > Now I want the function to take as argument a pointer to another instance[/color]
        of[color=blue]
        > the same function type. This will allow for removing functions from the
        > chain even in a different order than the one functions were installed, by
        > having each installed function recognize it's own address in the argument
        > and then return as a result the previous installed function that it knows[/color]
        of[color=blue]
        >
        > I hope you are following me...
        >
        > So how could I declare the prototype for a function that takes as argument
        > and that returns a pointer to the same function type being declared ?[/color]



        Comment

        • Andrew Koenig

          #5
          Re: Can I write a recursive type definition ?

          "Timothy Madden" <batman@rmv.spa m.home.ro> wrote in message
          news:2s2jbtF1gb 3abU1@uni-berlin.de...
          [color=blue]
          > I'm facing a very strange problem. I need to define a function that takes
          > as
          > argument something like a pointer to itself. It takes as argument a
          > pointer
          > of the same type as a pointer to itself and with possibly other
          > pointer-to-function value. But I don't have a data type for such a pointer
          > because the function prototype is in the same time being defined. I hope
          > you
          > are following me ...[/color]

          You can't do it directly, but you can wrap it in a structure:

          struct Selfptr {
          Selfptr (*fp)(Selfptr);
          };


          Comment

          • Keith Thompson

            #6
            Re: Can I write a recursive type definition ?

            "Andrew Koenig" <ark@acm.org> writes:[color=blue]
            > "Timothy Madden" <batman@rmv.spa m.home.ro> wrote in message
            > news:2s2jbtF1gb 3abU1@uni-berlin.de...
            >[color=green]
            >> I'm facing a very strange problem. I need to define a function that takes
            >> as
            >> argument something like a pointer to itself. It takes as argument a
            >> pointer
            >> of the same type as a pointer to itself and with possibly other
            >> pointer-to-function value. But I don't have a data type for such a pointer
            >> because the function prototype is in the same time being defined. I hope
            >> you
            >> are following me ...[/color]
            >
            > You can't do it directly, but you can wrap it in a structure:
            >
            > struct Selfptr {
            > Selfptr (*fp)(Selfptr);
            > };[/color]

            Unfortunately, this thread is cross-posted to comp.lang.c and
            comp.lang.c++. In C, you need:

            struct Selfptr {
            struct Selfptr (*fp)(struct Selfptr);
            };

            (gcc gives me "warning: parameter has incomplete type".)

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

            • Mabden

              #7
              Re: Can I write a recursive type definition ?

              "Timothy Madden" <batman@rmv.spa m.home.ro> wrote in message
              news:2s2jbtF1gb 3abU1@uni-berlin.de...[color=blue]
              > [Cross-posted to: comp.lang.c, comp.lang.c++]
              >
              > Hello all
              >
              > I'm facing a very strange problem. I need to define a function that[/color]
              takes as[color=blue]
              > argument something like a pointer to itself. It takes as argument a[/color]
              pointer[color=blue]
              > of the same type as a pointer to itself and with possibly other
              > pointer-to-function value. But I don't have a data type for such a[/color]
              pointer[color=blue]
              > because the function prototype is in the same time being defined. I[/color]
              hope you[color=blue]
              > are following me ...[/color]

              I think I see what you are trying to do. I once had to implement a form
              with fields that would a number of validation functions attached. If any
              one failed the chain was stopped, otherwise they would have to run down
              the list of functions.

              We implemented the validations as separate functions, and put them all
              in a validations array. The array was of type "pointer to function". Now
              all you need to is set the list of validations to the array offset. Call
              Val101, Val5, Val72, etc. Of course the names were #defined to something
              meaningful like ALL_CHARS, ZIPCODE, TELEPHONE_NUM, etc.

              In your case, you pass a (doubly) linked list of array offsets and add
              or remove items as needed. You could even restart the loop by setting
              curr_ftn to head.

              --
              Mabden


              Comment

              • Timothy Madden

                #8
                Re: Can I write a recursive type definition ?


                "Andrew Koenig" <ark@acm.org> wrote in message
                news:SUc7d.6526 55$Gx4.79218@bg tnsc04-news.ops.worldn et.att.net...[color=blue]
                > "Timothy Madden" <batman@rmv.spa m.home.ro> wrote in message
                > news:2s2jbtF1gb 3abU1@uni-berlin.de...
                >[color=green]
                > > I'm facing a very strange problem. I need to define a function that[/color][/color]
                takes[color=blue][color=green]
                > > as
                > > argument something like a pointer to itself. It takes as argument a
                > > pointer
                > > of the same type as a pointer to itself and with possibly other
                > > pointer-to-function value. But I don't have a data type for such a[/color][/color]
                pointer[color=blue][color=green]
                > > because the function prototype is in the same time being defined. I hope
                > > you
                > > are following me ...[/color]
                >
                > You can't do it directly, but you can wrap it in a structure:
                >
                > struct Selfptr {
                > Selfptr (*fp)(Selfptr);
                > };[/color]

                I think this is the best way I can find to do it ... with some operators
                overloaded in C++
                Thank you

                Does anyone think that there should be a special class of "void code
                pointes" in C/C++ ?
                A kind of pointer that could only be assigned function addresses, without
                the pointer being of a specific function type.

                Timothy Madden
                Romania
                -----------------------------------------------
                And I don't wanna miss a thing


                Comment

                • Victor Bazarov

                  #9
                  Re: Can I write a recursive type definition ?

                  Timothy Madden wrote:[color=blue]
                  > [...]
                  > Does anyone think that there should be a special class of "void code
                  > pointes" in C/C++ ?
                  > A kind of pointer that could only be assigned function addresses, without
                  > the pointer being of a specific function type.
                  >[/color]

                  What problem would you solve with it that cannot otherwise be solved
                  with what C++ already has?

                  V

                  Comment

                  • Timothy Madden

                    #10
                    Re: Can I write a recursive type definition ?


                    "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
                    news:5ic8d.3926 $Ae.1392@newsre ad1.dllstx09.us .to.verio.net.. .[color=blue]
                    > Timothy Madden wrote:[color=green]
                    > > [...]
                    > > Does anyone think that there should be a special class of "void code
                    > > pointes" in C/C++ ?
                    > > A kind of pointer that could only be assigned function addresses,[/color][/color]
                    without[color=blue][color=green]
                    > > the pointer being of a specific function type.
                    > >[/color]
                    >
                    > What problem would you solve with it that cannot otherwise be solved
                    > with what C++ already has?
                    >[/color]

                    Well ... It's like 'what problem would I solve with C++ that cannot
                    otherwise be solved with what C already has ?'
                    I think this could help making things clear in a good program

                    Timothy Madden
                    Romania
                    -----------------------------------------------------------
                    And I don't wanna miss a thing


                    Comment

                    • Michael Mair

                      #11
                      Re: Can I write a recursive type definition ?

                      Hi Timothy,


                      [Timothy Madden:][color=blue][color=green][color=darkred]
                      >>>Does anyone think that there should be a special class of "void code
                      >>>pointes" in C/C++ ?
                      >>>A kind of pointer that could only be assigned function addresses,
                      >>>without the pointer being of a specific function type.[/color]
                      >>[/color][/color]
                      [Victor Bazarov:][color=blue][color=green]
                      >>What problem would you solve with it that cannot otherwise be solved
                      >>with what C++ already has?[/color]
                      >[/color]
                      [Timothy Madden:][color=blue]
                      > Well ... It's like 'what problem would I solve with C++ that cannot
                      > otherwise be solved with what C already has ?'[/color]

                      I think the question here is what kind of "problem" we are talking
                      about. We could throw out some stuff from C and would still be able
                      to solve the same problems.
                      OTOH, C++ often speeds up development. And I think that it is here
                      that there is no bottleneck and no obvious gap which this addition
                      would help do away with. IMO, that is what Victor was addressing.

                      [color=blue]
                      > I think this could help making things clear in a good program[/color]

                      How so? You would get yourself function pointers without type
                      which would make it extremely hard to be sure that you are
                      calling a nondescript function correctly. IMO, a "collection " of
                      functions with the same return type and parameter list is the
                      only sensible case for the use of a function pointer. It is
                      clearer to provide wrapper functions for functions who do not
                      exactly fit than to have to take care which function now is
                      "behind" the "void *"-like function pointer.
                      Even if you could somehow derive return type and parameter list,
                      the code would not necessarily become clearer.


                      Cheers
                      Michael

                      Comment

                      • Tom Widmer

                        #12
                        Re: Can I write a recursive type definition ?

                        On Mon, 4 Oct 2004 14:53:00 +0200, "Timothy Madden"
                        <batman@rmv.spa m.home.ro> wrote:
                        [color=blue]
                        >
                        >"Andrew Koenig" <ark@acm.org> wrote in message
                        >news:SUc7d.652 655$Gx4.79218@b gtnsc04-news.ops.worldn et.att.net...[color=green]
                        >> "Timothy Madden" <batman@rmv.spa m.home.ro> wrote in message
                        >> news:2s2jbtF1gb 3abU1@uni-berlin.de...
                        >>[color=darkred]
                        >> > I'm facing a very strange problem. I need to define a function that[/color][/color]
                        >takes[color=green][color=darkred]
                        >> > as
                        >> > argument something like a pointer to itself. It takes as argument a
                        >> > pointer
                        >> > of the same type as a pointer to itself and with possibly other
                        >> > pointer-to-function value. But I don't have a data type for such a[/color][/color]
                        >pointer[color=green][color=darkred]
                        >> > because the function prototype is in the same time being defined. I hope
                        >> > you
                        >> > are following me ...[/color]
                        >>
                        >> You can't do it directly, but you can wrap it in a structure:
                        >>
                        >> struct Selfptr {
                        >> Selfptr (*fp)(Selfptr);
                        >> };[/color]
                        >
                        >I think this is the best way I can find to do it ... with some operators
                        >overloaded in C++
                        >Thank you
                        >
                        >Does anyone think that there should be a special class of "void code
                        >pointes" in C/C++ ?
                        >A kind of pointer that could only be assigned function addresses, without
                        >the pointer being of a specific function type.[/color]

                        Well, it is legal to cast function pointer types to other types. If I
                        want a generic pointer to function, I just use void(*)(void). Just
                        make sure you cast the function back to its original type before
                        calling it!

                        Tom

                        Comment

                        • Attila Feher

                          #13
                          Re: Can I write a recursive type definition ?

                          Tom Widmer wrote:
                          [SNIP][color=blue]
                          > Well, it is legal to cast function pointer types to other types.[/color]
                          [SNIP]

                          I guess you meant other function pointer types?

                          --
                          Attila aka WW


                          Comment

                          • Tom Widmer

                            #14
                            Re: Can I write a recursive type definition ?

                            On Tue, 5 Oct 2004 14:15:55 +0300, "Attila Feher"
                            <attila.feher@l mf.ericsson.se> wrote:
                            [color=blue]
                            >Tom Widmer wrote:
                            >[SNIP][color=green]
                            >> Well, it is legal to cast function pointer types to other types.[/color]
                            >[SNIP]
                            >
                            >I guess you meant other function pointer types?[/color]

                            Right.

                            Tom

                            Comment

                            Working...