Unusual template specialisation behavior
Matthias Walter
walter at mail.math.uni-magdeburg.de
Fri Feb 8 09:29:10 PST 2008
Sergey Gromov Wrote:
> Christopher Wright <dhasenan at gmail.com> wrote:
> > Matthias Walter wrote:
> > > Hello guys,
> > >
> > > Here's a (minimal) code snippet for my problem:
> > >
> > > | struct Expression (R, E) { }
> > > |
> > > | struct ResolveReference (T)
> > > | {
> > > | alias T reference_type;
> > > | }
> > > |
> > > | struct ResolveReference (T : Expression !(R, E))
> >
> > The compiler should have caught this, I think. You have R and E, but
> > they aren't defined. They default to int (which is why you saw that
> > behavior), but not being defined, they should be a compile-time error.
> >
> > The proper way to do this, unfortunately, involves static if rather than
> > overloading. At least if you want your users to supply a single argument
> > in all cases.
>
> struct ResolveReference (T : Expression !(R, E), R, E)
> {
> alias T* reference_type;
> }
>
> works just fine:
>
> void main (char[][] args)
> {
> printRefType !(uint);
> printRefType !(int);
> printRefType !(long);
> printRefType !(char);
> printRefType !(float);
>
> Stdout (ResolveReference !(uint).reference_type.stringof).newline;
> }
>
> Expression!(uint,uint)*
> Expression!(int,int)*
> Expression!(long,long)*
> Expression!(char,char)*
> Expression!(float,float)*
> uint
>
> --
> SnakE
Thank you both! I guess, this works, because when passing an Expression !(R, E), the 2nd and 3rd specialisation parameters R and E are deduced and everything works fine, right? Thanks for your help!
best regards
Matthias Walter
More information about the Digitalmars-d-learn
mailing list