trusted purity?
Kenji Hara
k.hara.pg at gmail.com
Mon Apr 29 19:19:36 PDT 2013
Hmm. Interesting approach. I tried to utilize the "trusted pure" concept .
template TrustedPure(alias func)
{
import std.traits, std.algorithm;
alias F1 = FunctionTypeOf!(func);
static if (functionAttributes!F1 & FunctionAttribute.pure_)
{
alias TrustedPure = func;
}
else
{
alias F2 = SetFunctionAttributes!(
F1,
functionLinkage!F1,
functionAttributes!F1 | FunctionAttribute.pure_);
auto ref TrustedPure(A...)(auto ref A args)
pure // mark as expected
@system // represent 'unsafe' operation.
{
// forward!args does not work, because
// std.algorithm.move is not pure...
return (cast(F2*)&func)(/*forward!*/args);
}
}
}
void main() pure
// cannot add @safe, because TrustedPure functions are always @system
{
import core.stdc.stdlib;
alias pmalloc = TrustedPure!(core.stdc.stdlib.malloc);
alias pfree = TrustedPure!(core.stdc.stdlib.free);
auto p = cast(int*)pmalloc(int.sizeof);
*p = 100;
pfree(p);
}
Kenji Hara
2013/4/30 monarch_dodra <monarchdodra at gmail.com>
> I'm getting strange behavior trying to cast to pure. This is my
> test program:
>
> //--------
> import std.stdio;
> import core.stdc.stdlib;
>
> void main()
> {
> auto p1 = &core.stdc.stdlib.free;
> auto p2 = cast(void function(void*))&core.stdc.**stdlib.free;
> auto p3 = cast(void function(void*)
> pure)&core.stdc.stdlib.free;
> auto pp1 = core.stdc.stdlib.malloc(5);
> auto pp2 = core.stdc.stdlib.malloc(5);
> auto pp3 = core.stdc.stdlib.malloc(5);
> writeln(p1);
> p1(pp1);
> writeln(p2);
> p2(pp2); //This hangs
> writeln(p3); //Never reaches here
> p3(pp3);
> }
> //--------
>
> Am I doing something wrong? Could somebody else test this? I'm on
> win32.
>
> I've also been getting some object violations trying to use this
> cast...
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130430/4815ee38/attachment.html>
More information about the Digitalmars-d
mailing list