Dereferencing void pointers: why not?
Vladimir Panteleev
thecybershadow at gmail.com
Tue Sep 18 19:08:12 PDT 2007
The Pascal/Delphi language has a nice feature - it can operate (within limits) on typeless data. This is similar to void pointers, but without the pointer clutter (so is in effect "void references"). Some examples:
1) Using a void pointer reference
Unlike in C/C++/D, you can dereference a void pointer (which in Pascal is represented by the "Pointer" type).
This allows me to write instead of:
PInteger(P)^ := 5;
this:
Integer(P^) := 5;
The difference in Pascal is more significant since you must define an integer pointer type :)
In a C-like language, it would be from:
*cast(int*)p = 5;
to
cast(int)*p = 5;
Less asterisks, less strain to the eye :) This may become worth it when the cast expressions grow long and nested.
2) Typeless function parameters
You can write something like this:
procedure Test(var Data);
Any mutable (non-const) value converts implicitely to that "void" type.
You can also use "const" instead of "var".
I guess in D it would be declaring such a function argument using "ref void Data", OSLT.
What can you do with such a reference? Well,
- you can cast it to something immediately useable
- you can get the address of it (void*)
- you can pass it on to other functions
Thus, it's just a little bit of syntax sugar and flexibility that fits in nicely in the type system.
--
Best regards,
Vladimir mailto:thecybershadow at gmail.com
More information about the Digitalmars-d
mailing list