January 31, 2016 casting & templates | ||||
---|---|---|---|---|
| ||||
I have: class OperatorV(T) : Value { T impl; this(T impl) { this.impl = impl; } ... and use it like this: makeOperator((IntV a, IntV b) => new IntV(a.num + b.num)); Now I want to do: opWord.get() returns a Value OperatorV *op = cast(OperatorV*)(opWord.get()); and get: Error: class app.OperatorV(T) is used as a type Hmm... any idea? Don't know how to carry the template stuff around. -- Robert M. Münch http://www.saphirion.com smarter | better | faster |
January 31, 2016 Re: casting & templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert M. Münch | On Sun, 31 Jan 2016 19:59:01 +0100, Robert M. Münch wrote:
> I have:
>
> class OperatorV(T) : Value {
> T impl;
>
> this(T impl) {
> this.impl = impl;
> }
> ...
This expands to:
template OperatorV(T) {
class OperatorV {
...
}
}
If you're just typing `OperatorV` with no template arguments, you're referring to the template itself.
There's not necessarily any relationship between two instantiations of a template with different arguments. You could write it so that OperatorV! int and OperatorV!string have entirely different sets of methods, constructors, and fields.
Because of that, the compiler doesn't create a base class for you automatically. But you can do it yourself:
class BaseOperator {} // or interface, or abstract class
class OperatorV(T) : BaseOperator {}
|
Copyright © 1999-2021 by the D Language Foundation