Re: Needed features in upcoming standards
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:
1. Computed goto
Many languages originally had this; over time they abandoned it.
There may be a reason why. Think on that.
I will add
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.
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; 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.
If a container holds data of different kinds one could use a tag
system, e.g.,
struct polydata {
enum tagtype type;
generic datum;
};
Richard Harter, cri@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.