Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
August 04, 2010 Recursive templated structs disallowed? | ||||
---|---|---|---|---|
| ||||
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?
--
Simen
|
August 04, 2010 Re: Recursive templated structs disallowed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | On Wed, 04 Aug 2010 15:37:32 -0400, Simen kjaeraas <simen.kjaras@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
|
August 04, 2010 Re: Recursive templated structs disallowed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Steven Schveighoffer <schveiguy@yahoo.com> wrote: > On Wed, 04 Aug 2010 15:37:32 -0400, Simen kjaeraas <simen.kjaras@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. I sorta do. I have a struct that keeps track of the operations performed on it, to create a template stack for calculating stuff at the end. Basically, I build a function by performing the operations on the struct. Not sure if this is an expression template, but possibly related. -- Simen |
August 04, 2010 Re: Recursive templated structs disallowed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer Attachments:
| On Wed, Aug 4, 2010 at 22:06, Steven Schveighoffer <schveiguy@yahoo.com>wrote:
> On Wed, 04 Aug 2010 15:37:32 -0400, Simen kjaeraas <simen.kjaras@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
>
I could get this to work, using a factory function:
import std.stdio;
struct Bar( T ) {
auto baz( U )( U arg ) {
return bar(this);
}
}
Bar!T bar(T)(T t)
{
return Bar!T();
}
void main( ) {
Bar!int n;
auto nn = n.baz( 3 );
writeln(typeof(n).stringof);
writeln(typeof(nn).stringof);
}
It's... fragile. Changes a few things here and there, and it's back to infinite recursion.
Philippe
|
August 05, 2010 Re: Recursive templated structs disallowed? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen kjaeraas | Simen kjaeraas 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?
>
Duplicate of bug 3869.
|
Copyright © 1999-2021 by the D Language Foundation