Template magic exercise
Artur Skawina
art.08.09 at gmail.com
Sun Mar 30 15:21:32 PDT 2014
On 03/30/14 15:36, Jack Applegame wrote:
> Wraper structure again. Is there solution without it?
No. D only allows op overloading in structs/unions/classes, so
one of those will be necessary. The simpliest solution would be
something like:
class C {
int elementsAccessor(size_t index) { ... }
@property ref elements() {
struct Elements(O) {
auto opIndex(size_t i) {
auto o = *cast(O*)&this;
return o.elementsAccessor(i);
}
@disable this(this);
}
return *cast(Elements!(typeof(this))*)&this;
}
}
and if you need that code to be @safe:
auto opIndex(size_t i) @safe {
auto o = (f) @trusted { return *cast(O*)f; }(&this);
return o.elementsAccessor(i);
}
// etc
artur
More information about the Digitalmars-d-learn
mailing list