Getting around the non-virtuality of templates
Stewart Gordon
smjg_1998 at yahoo.com
Tue Mar 27 08:12:13 PDT 2012
On 26/03/2012 14:37, Steven Schveighoffer wrote:
<snip>
> So for now, I use the undocumented old-style functions. One other thing that this
> "wrapper" method loses is covariance, which I use a lot in dcollections. I haven't filed a
> bug on it, but there is at least a workaround on this one -- the template can capture the
> type of "this" from the call site as a template parameter.
I can't seem to get this to work at the moment:
----------
class Base {
T method(T = typeof(this))() {
return this;
}
}
class Derived : Base {}
void main() {
Base b = new Base;
Derived d = new Derived;
auto bm = b.method();
auto dm = d.method();
b = d;
auto dbm = b.method();
pragma(msg, typeof(bm));
pragma(msg, typeof(dm));
pragma(msg, typeof(dbm));
}
----------
C:\Users\Stewart\Documents\Programming\D\Tests>dmd typeof_this
Base
Base
Base
----------
Or do you mean something else?
<snip>
> Set opAnd(E)(bool delegate(E) dg) if(!is(E == Element) && implicitlyConvertsTo!(E, Element))
> {
> bool _dg(Element e)
> {
> return dg(e);
> }
> // call protected virtual opAnd equivalent with &_dg
> }
<snip>
I was beginning to think along those lines myself. I'll try it and see how I get on.
Stewart.
More information about the Digitalmars-d
mailing list