Comment #2 by bearophile_hugs — 2013-11-27T00:46:46Z
Do you mean in this code:
import core.stdc.stdlib, std.stdio;
struct VariableLength(T) {
size_t len;
T[0] items;
@property T[] data() inout pure nothrow {
return (cast(T*)&items)[0 .. len];
}
static typeof(this)* New(in size_t len) nothrow {
auto vptr = cast(typeof(this)*)calloc(1,
typeof(this).sizeof +
T.sizeof * len);
vptr.len = len;
return vptr;
}
alias data this;
}
void main() {
enum n = 0100;
auto v = VariableLength!int.New(n);
foreach (immutable i; 0 .. n)
(*v)[i] = i * 10;
foreach (immutable i; 0 .. n)
writeln((*v)[i]);
}
The data() function will be written just like this?
@property T[] data() inout pure nothrow {
return items.ptr[0 .. len];
}
Comment #3 by yebblies — 2013-11-27T01:37:22Z
(In reply to comment #2)
> Do you mean in this code:
>
> [snip]
>
>
> The data() function will be written just like this?
>
> @property T[] data() inout pure nothrow {
> return items.ptr[0 .. len];
> }
Looks like you'll still need to cast away inout in that example, but yeah.
Where a is a zero-length static array a.ptr -> &a
Comment #4 by github-bugzilla — 2013-12-01T17:57:27Z
Comment #6 by github-bugzilla — 2014-09-15T07:23:17Z
Commit pushed to master at https://github.com/D-Programming-Language/dmdhttps://github.com/D-Programming-Language/dmd/commit/b7866eff1567e5dfdf0e28e99d6cb3515d273854
Fix bogus zero-length array test.
Issue 7175 was about .ptr always giving null for a zero-length
static array in a struct, despite the fact that the struct has
size 1 and thus certainly a non-null address.
The test case as modified by 36c9b64, however, checks that a
free-standing zero-length static array on the stack has an
address, which is quite a different scenario. From the spec,
I don't see a reason why zero-sized objects should need any
memory associated with them, so I am reluctant to pessimize
LDC's codegen over this.