Recursive template expansion

Norbert Nemec Norbert at Nemec-online.de
Mon Mar 1 08:26:46 PST 2010


Thanks, Philippe, for the elegant workaround!

Still I would like to know the reason for my original failure, 
especially since the compiler error message did not contain any line 
number whatsoever. Getting this kind of error message in a large project 
must be really ugly to solve, even if the workaround is so 
straightforward...




Philippe Sigaud wrote:
> 
> 
> On Mon, Mar 1, 2010 at 10:16, Norbert Nemec <Norbert at nemec-online.de 
> <mailto:Norbert at nemec-online.de>> wrote:
> 
> 
>     Is there any fundamental error in my thinking? Some simple
>     misunderstanding? Some slightly different syntax to be used? Or is
>     it simply an unnessessary restriction in the compiler that could
>     easily be removed?
> 
> 
> I don't know. Using factory functions, it seems to work and it has the 
> nice side-effect of simplfying the syntax (templated functions do all 
> the type deducing):


> 
> import std.stdio;
> 
> struct Sum(A,B) {
>    A a;
>    B b;
> 
>    auto opAdd(T)(T a) { return sum(this,a); }
> }
> 
> 
> struct Base {
>    auto opAdd(T)(T a) { return sum(this,a); }
> }
> 
> Sum!(A,B) sum(A,B)(A a, B b) { return Sum!(A,B)(a,b);}
> Base!(A,B) base(A,B)(A a, B b) { return Base!(A,B)(a,b);}
> 
> void main() {
>    Base a,b,c;
> 
>    auto d = a+b;
>    writeln(typeof(e).stringof); // Sum!(Base, Base)
>    auto e =de+a;
>    writeln(typeof(f).stringof); // Sum!(Sum!(Base, Base),Base)
>    auto f = e+e+d;              // Look Ma, no parenthesis
>    writeln(typeof(g).stringof); // Sum!(Sum!(Sum!(Sum!(Base,Base),Base), 
> Sum!(Sum!(Base,Base),Base)),Sum!(Base,Base))
> }
>  
> 
> Cheers,
> 
>   Philippe
> 
> 



More information about the Digitalmars-d mailing list