using memset withing a pure function
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Aug 14 18:32:21 PDT 2015
On 08/14/2015 06:09 PM, D_Learner wrote:
> When writting a pure fucntion involving C non pure functions
If you want to live dangerously, you can use assumePure, which is found
in one of the unittest blocks of std.traits:
import std.traits;
auto assumePure(T)(T t)
if (isFunctionPointer!T || isDelegate!T)
{
enum attrs = functionAttributes!T | FunctionAttribute.pure_;
return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t;
}
int i = 0;
void foo()
{
++i; // foo accesses mutable module-level data
}
void bar() pure
{
auto pureFoo = assumePure(&foo);
pureFoo(); // <-- pure function is calling impure function
}
void main()
{
assert(i == 0);
bar();
assert(i == 1); // mutation through a pure function
}
Ali
More information about the Digitalmars-d-learn
mailing list