Re: Templates for instantiating derived class

2021-09-20 Thread Steven Schveighoffer via Digitalmars-d-learn
On 9/20/21 6:16 PM, rjkilpatrick wrote: Essentially, I would like to write a template that calls the constructor of the parent class or the constructor of the inherited class, depending on its type. ... Some kind of `return new this(...)` would be good, but that's not possible. I think it

Re: Templates for instantiating derived class

2021-09-20 Thread Adam Ruppe via Digitalmars-d-learn
On Monday, 20 September 2021 at 22:16:47 UTC, rjkilpatrick wrote: auto opBinary(string op)(int rhs) const if (op == "+") { return new Super(_a + rhs); // Creates of type Super even when called from derived class } Make this auto opBinary(string op, this This)(int rhs)

Templates for instantiating derived class

2021-09-20 Thread rjkilpatrick via Digitalmars-d-learn
Essentially, I would like to write a template that calls the constructor of the parent class or the constructor of the inherited class, depending on its type. ```d #!/usr/bin/env dub /+ dub.sdl: name "mwe" +/ class Super { private int _a; this(){} this(int a) { _a =