Template type reduction
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Aug 15 12:40:37 PDT 2016
On 8/15/16 3:31 PM, Engine Machine wrote:
> Suppose I have a templated type like
>
> struct S(T) { int x; static if (T is Y) int y; }
>
> I would like to be able to create a reference to S(T) for any T,
>
> struct Q
> {
> S!* s; // Can hold any type of S.
> }
>
> and be able to access s.x, since it is common to all S.
>
> Can D do anything like this? It is sort of like runtime inheritance, but
> at the compile time level.
I don't think so. You'd have to cast, as the compiler doesn't have any
understanding that all S instantiations will have an x member.
> I do not want to have to cast to S!T every time just to access x, e.g.,
>
> struct Q
> {
> Object s;
> }
>
> which is too general as s can be things that are not of type S!*.
This seems odd. You will lose the type information for s if you were to
succeed. Might as well just store an int.
Seems like what you want is a variant or some other kind of tagged union.
-Steve
More information about the Digitalmars-d-learn
mailing list