Unusual template specialisation behavior

Sergey Gromov snake.scaly at gmail.com
Fri Feb 8 08:37:31 PST 2008


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


More information about the Digitalmars-d-learn mailing list