Templates, D way
Computermatronic via Digitalmars-d
digitalmars-d at puremagic.com
Tue Sep 5 05:41:45 PDT 2017
On Tuesday, 5 September 2017 at 12:20:14 UTC, crimaniak wrote:
> On Tuesday, 5 September 2017 at 11:08:57 UTC, Void-995 wrote:
>> @property mixin(DataList!("firstSublist",
>> MyBinarySubStructAForA, firstSublistMembersCount,
>> firstSublistMembersOffset));
> I don't think string mixins are required here. It seems just
> template is more simple.
>
> T[] getBytesAs(T, alias length, alias offset)()
> {
> return (cast(T *)(cast(byte *)(&this) + offset))[0 ..
> length];
> }
>
> struct MyBinaryStructA
> {
> ...
> alias firstList = getBytesAs!(MyBinarySubStructAForA,
> firstSublistMembersCount, firstSublistMembersOffset);
> alias secondList = getBytesAs!(MyBinarySubStructBForA,
> secondSublistMembersCount, secondSublistMembersOffset);
> }
>
> unittest
> {
> ...
> MyBinaryStructA *binaryData = cast(MyBinaryStructA
> *)fileData.ptr;
>
> auto a = binaryData.firstList;
> }
I find it very strange that this works, as a non-mixin template
should not be able to capture the context of where it was
instantiated. If you take the alias template parameters out it
behaves how it should (that is an error message saying this is
not accessible).
More information about the Digitalmars-d
mailing list