scope as template struct
Christian Kamm
kamm-incasoftware at removethis.de
Tue Jan 13 13:45:00 PST 2009
With the features of D2 you can get pretty close to implementing scope
classes as a template struct:
private final class PlacementNewClass(T) : T
{
// forward constructors here!
new(uint size, byte[__traits(classInstanceSize, PlacementNewClass)] mem) {
return &mem;
}
delete(void* addr) {}
}
struct StackClass(T)
{
// forward constructors here!
void construct() {
new(memory) PlacementNewClass!(T);
}
~this() {
T tmp = cast(T)this;
delete tmp;
}
// should be opImplicitCast
T opCast() { return cast(T)cast(void*)&memory; }
T opDot() { return cast(T)cast(void*)&memory; }
byte[__traits(classInstanceSize, PlacementNewClass!(T))] memory;
}
Instances of StackClass!(C) behave pretty much like scope C, except they are
not limited to function local variables.
Unless there's a major difference to 'scope' I missed, I think that with the
addition of a few generic features it should be possible to move it into a
library entirely. In particular, D would need opImplicitCast as well as a
way of accessing and forwarding constructors to make this work nicely.
More information about the Digitalmars-d
mailing list