<div dir="ltr">Hmm. Interesting approach. I tried to utilize the "trusted pure" concept .<div><br></div><div><div>template TrustedPure(alias func)</div><div>{</div><div>    import std.traits, std.algorithm;</div>
<div><br></div><div>    alias F1 = FunctionTypeOf!(func);</div><div>    static if (functionAttributes!F1 & FunctionAttribute.pure_)</div><div>    {</div><div>        alias TrustedPure = func;</div><div>    }</div><div>
    else</div><div>    {</div><div>        alias F2 = SetFunctionAttributes!(</div><div>            F1,</div><div>            functionLinkage!F1,</div><div>            functionAttributes!F1 | FunctionAttribute.pure_);</div>
<div><br></div><div>        auto ref TrustedPure(A...)(auto ref A args)</div><div>        pure    // mark as expected</div><div>        @system // represent 'unsafe' operation.</div><div>        {</div><div>            // forward!args does not work, because</div>
<div>            // std.algorithm.move is not pure...</div><div>            return (cast(F2*)&func)(/*forward!*/args);</div><div>        }</div><div>    }</div><div>}</div><div>void main() pure</div><div>// cannot add @safe, because TrustedPure functions are always @system</div>
<div>{</div><div>    import core.stdc.stdlib;</div><div>    alias pmalloc = TrustedPure!(core.stdc.stdlib.malloc);<br></div><div>    alias pfree = TrustedPure!(core.stdc.stdlib.free);</div><div><br></div><div>    auto p = cast(int*)pmalloc(int.sizeof);</div>
<div>    *p = 100;</div><div>    pfree(p);</div><div>}<br></div></div><div><br></div><div>Kenji Hara</div><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">2013/4/30 monarch_dodra <span dir="ltr"><<a href="mailto:monarchdodra@gmail.com" target="_blank">monarchdodra@gmail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I'm getting strange behavior trying to cast to pure. This is my<br>
test program:<br>
<br>
//--------<br>
import std.stdio;<br>
import core.stdc.stdlib;<br>
<br>
void main()<br>
{<br>
     auto p1 = &core.stdc.stdlib.free;<br>
     auto p2 = cast(void function(void*))&core.stdc.<u></u>stdlib.free;<br>
     auto p3 = cast(void function(void*)<br>
pure)&core.stdc.stdlib.free;<br>
     auto pp1 = core.stdc.stdlib.malloc(5);<br>
     auto pp2 = core.stdc.stdlib.malloc(5);<br>
     auto pp3 = core.stdc.stdlib.malloc(5);<br>
     writeln(p1);<br>
     p1(pp1);<br>
     writeln(p2);<br>
     p2(pp2); //This hangs<br>
     writeln(p3); //Never reaches here<br>
     p3(pp3);<br>
}<br>
//--------<br>
<br>
Am I doing something wrong? Could somebody else test this? I'm on<br>
win32.<br>
<br>
I've also been getting some object violations trying to use this<br>
cast...<br>
</blockquote></div><br></div></div>