Recursive templated structs disallowed?

Steven Schveighoffer schveiguy at yahoo.com
Wed Aug 4 13:06:27 PDT 2010


On Wed, 04 Aug 2010 15:37:32 -0400, Simen kjaeraas  
<simen.kjaras at gmail.com> wrote:

> struct bar( T ) {
>      auto baz( U )( U arg ) {
>          bar!( typeof( this ) ) tmp;
>          return tmp;
>      }
> }
>
> void main( ) {
>      bar!int n;
>      n.baz( 3 );
> }
>
> This code fails with
> Error: recursive template expansion for template argument bar!(int)
> Now, I agree it is recursive, but it is not infinitely recursive, so
> there shouldn't really be a problem.
> Is this a bug? Is there a workaround?

It's lazily recursive.  Meaning, it *is* infinitely recursive, but the  
compiler does not have to evaluate all the recursions until they are used.

I'm not sure it should be disallowed, but it's definitely on the edge.  Do  
you have some real-world case for this?  If you want to get something like  
this fixed, you'll need a good example, not an academic one.

-Steve


More information about the Digitalmars-d-learn mailing list