Is this a compiler error? "recursive template expansion"

Nathan S. no.public.email at example.com
Tue Dec 8 20:11:40 UTC 2020


The following code fails to compile. Is this a compiler error or 
if not what is wrong with the code?

---
struct Template2(T)
{
     // If both of the following are removed compilation succeeds
     // without any other changes:
     enum tString = T.stringof;
     static if (is(T == class))
         enum tLinkage = __traits(getLinkage, T);
}

struct Template1(Param1, Param2 = Template2!Param1) {}

// Moving the definition of AliasTemplate1S after the definition 
of S
// causes compilation to succeed without any other changes.
alias AliasTemplate1S = Template1!S;

class S
{
     // If the following line is removed compilation succeeds
     // without any other changes.
     Template1!int x;
}

void main()
{
}
---

Failure message:
main.d(20): Error: struct main.Template1(Param1, Param2 = 
Template2!Param1) recursive template expansion
main.d(20):        while looking for match for Template1!int
main.d(7): Error: class S is forward referenced
main.d(10): Error: template instance main.Template2!(S) error 
instantiating
main.d(14):        instantiated from here: Template1!(S)


More information about the Digitalmars-d-learn mailing list