std.file.read returns void[] why?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Apr 17 14:27:44 PDT 2014


On Thu, 17 Apr 2014 17:04:25 -0400, monarch_dodra <monarchdodra at gmail.com>  
wrote:

> On Thursday, 17 April 2014 at 12:59:20 UTC, Steven Schveighoffer wrote:
>> It was never possible. You must explicitly cast to void[].
>>
>> void[] makes actually little sense as the result of whole-file read  
>> that allocates. byte[] is at least usable and more accurate. In fact,  
>> it's a little dangerous to use void[], since you could assign  
>> pointer-containing values to the void[] and it should be marked as  
>> NOSCAN (no pointers inside file data).
>>
>> However, when using the more conventional read(void[]) makes a LOT of  
>> sense, since any T[] implicitly casts to void[].
>>
>> -Steve
>
> void[] will only make sense once you've accepted that "void.sizeof == 1".

It is already accepted that when we talk about length in a void[], it's  
the number of bytes. But the data has no formal type.

But any array implicitly casts to void[]. This is why it makes a good  
parameter for read or write (when reading or writing the binary data).

> Well, I guess "void[]" is C++'s "char*" for indiscriminate buffers.  
> Speaking of which, does "void*" trigger strict aliasing in D? This  
> subject seems like a hot potato no-one wants to touch.

No, it's equivalent to void *, not char *.

in D, ubyte[] would be the equivalent of C's char *.

-Steve


More information about the Digitalmars-d-learn mailing list