Recursive template expansion
Philippe Sigaud
philippe.sigaud at gmail.com
Mon Mar 1 08:13:18 PST 2010
On Mon, Mar 1, 2010 at 10:16, Norbert Nemec <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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100301/d1bdb955/attachment.html>
More information about the Digitalmars-d
mailing list