Re: Needed features in upcoming standards
"Richard Harter" <cri@tiac.net> wrote in message
news:49677c0b.686951765@text.giganews.com...
On Thu, 08 Jan 2009 02:48:53 GMT, "Bartc" <bartc@freeuk.com>
wrote:
"Richard Harter" <cri@tiac.net> wrote in message
news:49655258.545204953@text.giganews.com...
On Tue, 06 Jan 2009 11:13:08 -0600, Blake McBride
<blake@mcbride.name> wrote:
3. Generic variable type
I would like a generic generic variable type similar to void *
pointers that be anything that is not a struct. In otherwords it
is large enough to hold a long, any kind of pointer, a long long
if you have them, etc. It is convertible with casting. Thus you
might have
generic T;
long number;
char * s;
T = number;
number = T;
T = s;
s = T;
I think T would need a tag of some sort to remember what type it contains
(otherwise the use is limited).
But would the user have to interrogate the type, or would this be
automatic?
If the latter, this could be a very useful feature but also quite high
level
(maybe too much so) for C:
generic max(generic A, generic B) {
return A>=B ? A : B;
}
If A and B are pointers to different storage objects you have
just introduced undefined behaviour. We won't even think about A
and B being different types.
I don't quite get your last sentence. Probably we are thinking about
somewhat different things. My example assumes generic is a tagged type, and
that A, B refer to the data within the type, not the data structure
representing the type (such as your polydata struct below).
In that case a runtime check would ensure they were compatible for ">=". And
even if A,B happened to be pointers, and ">=" was allowed for pointers to
different types, then the runtime system would know if this was legal or not
for that platform.
However, my experience of implementing variant types tells me this would
be
quite difficult to get as efficient as dedicated types (although in this
case the possibilities are restricted to a small range of scalar types).
My take on this is that a generic would be like a void * pointer;
you can't dereference it. In C one wouldn't want tags as a
required feature;
No? So how would any piece of code know what a generic contained/pointed to?
Without a tag, I can't see this being too useful, and you might as well just
use the longest int available.
however you could create a tag structure in the
same way that you would do with unions.
Some people think of unions as a space saving device; however it
can be thought of as a device for implementing polymorphic data.
The typical use I see is in data structures that act as
containers, e.g., linked lists, hash tables, trees, etc. If the
contained data is all of the same type, the container code can be
reused without modification.
OK, so you're thinking more along the lines of templates.
If a container holds data of different kinds one could use a tag
system, e.g.,
struct polydata {
enum tagtype type;
generic datum;
};
Yes, and my question was whether dealing with this would be done manually
(which is more in line with how C works) or automatically by the runtime
system (which is nicer but more suited to a scripting language).
--
Bartc