recursive template expansion: Why does this not compile?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Wed Mar 21 01:04:21 UTC 2018


On Wednesday, March 21, 2018 00:47:18 Ontonator via Digitalmars-d-learn 
wrote:
> The following code does not compile:
> > void main() {}
> >
> > class SuperClass {}
> >
> > class TemplatedClass(T : SuperClass) {}
> >
> > class A : SuperClass {
> >
> >     alias T = TemplatedClass!B;
> >
> > }
> >
> > class B : SuperClass {
> >
> >     alias T = TemplatedClass!C;
> >
> > }
> >
> > class C : SuperClass {}
>
> It gives the error:
> > test.d(12): Error: class `test.TemplatedClass(T : SuperClass)`
> > recursive template expansion
> > test.d(12):        while looking for match for
> > TemplatedClass!(C)
>
> The aliases do not have to be aliases, as long as there is some
> reference to the class (e.g. method and variable declarations
> also work). What exactly is the reason for this error?

I'm not sure exactly what's happening, since I'm not very familiar with the
exactly how template specializations are defined, but the problem clearly
relates to the fact that you used a template specialization instead of a
template constraint. If you change TemplatedClass to

class TemplatedClass(T)
    if(is(T : SuperClass))
{}

then the code compiles.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list