Can I write a recursive type definition ?

T

Timothy Madden

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

Xenos

Timothy Madden said:
[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

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


Regards.
 
P

puppet_sock

Timothy Madden said:
But I don't have a data type for such a pointer
because the function prototype is in the same time being defined.

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
 
S

Siemel Naran

Timothy Madden said:
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 ...

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

Andrew Koenig

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

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

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

Keith Thompson

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

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

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".)
 
M

Mabden

Timothy Madden said:
[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 ...

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

Timothy Madden

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

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

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
 
V

Victor Bazarov

Timothy said:
[...]
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.

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

V
 
M

Michael Mair

Hi Timothy,


[Timothy Madden:]
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.
[Victor Bazarov:]
What problem would you solve with it that cannot otherwise be solved
with what C++ already has?
[Timothy Madden:]
Well ... It's like 'what problem would I solve with C++ that cannot
otherwise be solved with what C already has ?'

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.

I think this could help making things clear in a good program

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
 
T

Timothy Madden

Victor Bazarov said:
Timothy said:
[...]
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.

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

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
 
T

Tom Widmer

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top