www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Asymmetrical usage of void parameters

reply Kris <Kris_member pathlink.com> writes:
One may legitimally write the following:

void bind (inout void[] x)
{
// ...
}

Which will match any argument of type array. But one cannot do the following:

void bind (inout void x)
{
}

which is intended to catch all non-array types.

Shouldn't this be symmetrical? The alternative is to remove the 'inout' and make
the arguments pointers instead -- not an ideal resolution.

- Kris
Feb 12 2005
parent reply "Walter" <newshound digitalmars.com> writes:
"Kris" <Kris_member pathlink.com> wrote in message
news:cum3f4$2r23$1 digitaldaemon.com...
 One may legitimally write the following:

 void bind (inout void[] x)
 {
 // ...
 }

 Which will match any argument of type array. But one cannot do the

 void bind (inout void x)
 {
 }

 which is intended to catch all non-array types.

 Shouldn't this be symmetrical?

The void[] is analgous to the C++ void* type, except that it adds a length in bytes of whatever is being pointed to. void[] turns out to be a very handy data type. It means "block of memory 'length' bytes long".
 The alternative is to remove the 'inout' and make
 the arguments pointers instead -- not an ideal resolution.

I'm not sure what use this would be. Note that although C++ (and D) have a void* type, neither has a void& type.
Feb 12 2005
parent reply Kris <Kris_member pathlink.com> writes:
In article <cumblv$31ik$1 digitaldaemon.com>, Walter says...
"Kris" <Kris_member pathlink.com> wrote in message
news:cum3f4$2r23$1 digitaldaemon.com...
 One may legitimally write the following:

 void bind (inout void[] x)
 {
 // ...
 }

 Which will match any argument of type array. But one cannot do the

 void bind (inout void x)
 {
 }

 which is intended to catch all non-array types.

 Shouldn't this be symmetrical?

The void[] is analgous to the C++ void* type, except that it adds a length in bytes of whatever is being pointed to. void[] turns out to be a very handy data type. It means "block of memory 'length' bytes long".

Sure is!
 The alternative is to remove the 'inout' and make
 the arguments pointers instead -- not an ideal resolution.

I'm not sure what use this would be. Note that although C++ (and D) have a void* type, neither has a void& type.

Hmmmm. I was trying to use it to represent an 'anonymous element', in a similar manner as I've been using void[]. I can use void* instead, but it's not so nice for the user to add the & operator. Another option is to convert the argument to an array first, such as (&x)[0..1], and pass it as an anonymous array. I'll futz around some more; - Kris
Feb 12 2005
parent "Walter" <newshound digitalmars.com> writes:
"Kris" <Kris_member pathlink.com> wrote in message
news:cumcrc$154$1 digitaldaemon.com...
 In article <cumblv$31ik$1 digitaldaemon.com>, Walter says...
The void[] is analgous to the C++ void* type, except that it adds a


in bytes of whatever is being pointed to. void[] turns out to be a very
handy data type. It means "block of memory 'length' bytes long".

Sure is!

It's just a lucky epiphany I had one day struggling with byte[] being returned from std.file.read(), very obvious in hindsight! The other neat thing about void* in D is that one can do pointer arithmetic on it. It's always frustrating that in C/C++ one has to cast it to char* first, then back to void*. Dagnamit, it isn't a pointer to a char and that shouldn't be necessary! What you can't do with void* or void[] in D is dereference it.
Feb 12 2005