How to implement this?

Elvis Zhou elvis.x.zhou at gmail.com
Fri Apr 8 10:04:04 UTC 2022


On Friday, 8 April 2022 at 09:08:07 UTC, Stanislav Blinov wrote:
> On Friday, 8 April 2022 at 05:53:03 UTC, Elvis Zhou wrote:
>
>> assumeNoEscapeOrWhatever!DynamicArray structs;
>> structs ~= cast(A*)&b;
>>
>> is it possible?
>
> That's what `@trusted` is for. And that's also why it should be 
> used with care, and on the smallest code possible.
>
> ```d
> struct A {}
> struct B { A a; }
> struct C { A a; }
>
> void main()
> {
>     A*[] structs;
>
>     B b;
>     C c;
>
>     () @trusted {
>         structs ~= cast(A*)&b;
>         structs ~= cast(A*)&c;
>     } ();
> }
> ```

Thank you, this is exactly what I'm looking for!

But why this doesn't work?
void func() @trusted
{
     A*[] structs;
     B b;
     structs ~= cast(A*)&b; // still error
}



More information about the Digitalmars-d-learn mailing list