Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!mailrus!cornell!batcomputer!olson From: olson@batcomputer.tn.cornell.edu (olson) Newsgroups: comp.std.c Subject: Re: function returning pointer to itself Message-ID: <5489@batcomputer.tn.cornell.edu> Date: 13 Jul 88 06:41:26 GMT References: <5485@batcomputer.tn.cornell.edu> Reply-To: olson@tcgould.tn.cornell.edu (olson) Organization: Cornell Theory Center, Cornell University, Ithaca NY Lines: 79 In article <5485@batcomputer.tn.cornell.edu> olson@tcgould.tn.cornell.edu (olson) writes: >Does the new C standard have a natural way to declare a >function that returns a pointer to itself >???? Thanks to all the people that replied to my question. Everyone said effectively, "NO, this can not be done NATURALLY in C" (which is the response I expected) BUT you can mock it up by 1) using type cast 2) encapsulation in a data structure That C can't do this naturally bugs me. What do people think of the following proposal? Define a new primative type 'self' (not a very good name ...) self *p; means p points to something that points to something that ... or p points to itself p->p->p->p->p->... self *f(); means f is a function returning a pointer to a function returning a pointer to a ... or f is a function returning a pointer to itself f()->f()->f()->... self **f() means f is a function returning a pointer to a pointer to a function returning a pointer to a pointer to a ... or ??? f()->p->f()->p->f()->p->... self *(*p)() means p is a pointer to a function returning a pointer to a pointer to a function returning a pointer to a ... or ??? p->f()->p->f()->p->f()->... That is, 'self' sets up a definition that is "solved" by iteration (but that is NOT how you implement it) self *(*p)() => p=*(*p)() = *(*(*(*p)()))() = ... Note that self must always involve pointers, self p is not allowed. Further one could write p->f()->f()->f()->f()->... as typedef self *state(); state *p; Without typedefs this would not be possible to express, unless further syntax modifications were made, such as self *<*p>(); means p->f()->f()->f()->... self *; means f()->p->p->p->... where the <> indicate the part that is to be iterated on ie self * => =* rather than f = * SIDE ISSUE: (independent of self) Note in the same way that it is impossible to declare a function using the typedef typedef int myfunc(); because myfunc f(){}; means int (f())(){}; which is illegal, it is impossible to declare a function state f(){}; This is something that bugs me too. Would it screw up the intuitiveness of typedefs too much to define myfunc f(){}; to mean int f(){}; since the only other meaning is illegal???????? COMMENTS 1) I don't see how 'self' would pose any implementation problems. (on the other hand I have never written a compiler, only thought about it) 2) There have been enough occasions where I could have used this primative that I would call it a useful feature. 3) It might be opening a pandora's box. Consider, someone might say, well I want f()->p->f()->p->p->f()->p->p->p->f()->... or one where the number of p's are the digits of PI. Well, what can I say. These would be a lot harder to implement. I haven't been able to conceive of a use for them. 4) It is probably to late to add this primative to the new C standard. Todd Olson olson@helios.tn.cornell.edu Physics Dept/ Cornell Univ/ Ithaca NY 14853