Is there other way to do that?
evilrat
evilrat666 at gmail.com
Mon Feb 15 09:03:20 UTC 2021
On Monday, 15 February 2021 at 07:26:56 UTC, Jack wrote:
> I need to check if an instance is of a specific type derived
> from my base class but this class has template parameter and
> this type isn't available at time I'm checking it. Something
> like:
>
Non-templated interface/base class is probably the only way I'm
aware of, it is also how it is usually done with C# generics
where needed for the same reason.
Even though typeid() knows the type I have no idea how to use it
that way.
Interface adds 1 pointer to classinfo or vtbl, so it is increases
instance size a bit.
```
import std;
class B { }
class A(T) : B { }
class X : B { }
class Z : B { }
interface IA {}
class AA(T) : B, IA {}
void main()
{
writeln(__traits(classInstanceSize, AA!void)); // prints 24
writeln(__traits(classInstanceSize, A!void)); // prints 16
}
```
More information about the Digitalmars-d-learn
mailing list