C-binding external array.

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 10 08:07:52 PDT 2016


On 08/10/2016 02:05 AM, ciechowoj wrote:
> On Tuesday, 9 August 2016 at 19:16:42 UTC, Steven Schveighoffer wrote:
>>
>> D has an answer:
>>
>> pragma(mangle, "tab")
>> extern extern(C) int[1] _ctab;
>>
>> @property int* tab() { return _ctab.ptr; }
>>
>> I still don't recommend doing this, for previously stated reasons.
>>
>
> This is really interesting :).
>
Better with some mixin magic:

mixin template CArray(string symbol, T) {
     pragma(mangle, symbol) extern extern(C) __gshared
     mixin ("T[0] _c" ~ symbol ~ ";");

     @property
     mixin ("T* " ~ symbol ~ "() { return _c" ~ symbol ~ ".ptr; }");
}

mixin CArray!("tab", int);

tab[42] = 42;

Ali



More information about the Digitalmars-d-learn mailing list