Void initialization

Stewart Gordon smjg_1998 at yahoo.com
Tue Dec 20 13:29:56 PST 2011


On 20/12/2011 18:12, bearophile wrote:
<snip>
> Because in that code I have used three times a type (TF), auto allows to remove only
> one of them. The alias is not the best solution (a better solution is to put that code
> into a templated function), but repeating the same generic type more than one time is
> usually a source of bugs.

I don't quite understand - why not just use float as it is?  OK, so abbreviating it to TF 
saves 9 characters on that line, but the alias declaration and its trailing line break 
take up 16 characters, so you're not saving space at all.

Moreover, the style guide discourages meaningless type aliases.  (OK, so there are things 
I disagree with there, like using spaces not tabs for indentation, but that's another matter.)

>> - std.gc.malloc returns the array with correct length according to my quick test, so the
>> use of [0..x] is redundant
>
> Really? Well, as I have said I have not tested that code.
> Generally GC functions return a void*, so to create an array I think you need to slice
> it...

If that were the case, the OP's code wouldn't have compiled.  I made out that the OP was 
getting these errors at runtime, not compiletime.

> What is the code of your quick test?
<snip>

import std.stdio, std.gc;

void main() {
     size_t x = 42;

     float[] f = cast(float[]) std.gc.malloc(x*4);
     writefln(f.length);

     alias float TF;
     f = (cast(TF*)std.gc.malloc(x * TF.sizeof))[0 .. x];
     writefln(f.length);
}


Stewart.


More information about the Digitalmars-d-learn mailing list