please explain the spec: alias type specialization
Paul Thompson
p.thompson at mailinator.com
Mon Nov 2 23:09:47 PST 2009
== Quote from Russell Lewis (webmaster at villagersonline.com)'s article
> I have a set of templates which take alias parameters, and I want to
> specialize them based on the type of the aliased symbols.
> So I looked at
> http://digitalmars.com/d/2.0/template.html#TemplateAliasParameter and
> saw the following grammar (but no matching example):
> TemplateAliasParameter:
> alias Identifier
> alias Identifier TemplateAliasParameterSpecialization
> alias Identifier TemplateAliasParameterDefault
> alias Identifier TemplateAliasParameterSpecialization
> TemplateAliasParameterDefault
> TemplateAliasParameterSpecialization:
> : Type
> TemplateAliasParameterDefault:
> = Type
> So I wrote this code, which DMD rejects:
> template foo(alias A : int) {}
> How is this supposed to work? Or, is this a DMD bug?
> Russ
I was working through the grammar and found this too.
The document at:
http://www.digitalmars.com/d/1.0/template.html#TemplateAliasParameter
http://www.digitalmars.com/d/2.0/template.html#TemplateAliasParameter
says:
Template Alias Parameters
TemplateAliasParameter:
alias Identifier TemplateAliasParameterSpecializationopt TemplateAliasParameterDefaultopt
TemplateAliasParameterSpecialization:
: Type <---- ???
TemplateAliasParameterDefault:
= Type <---- ???
What DOES work is this:
//=======================================
int defaultSymbol;
int someParticularSymbol;
//generic version for any someThing defaulting to defaultSymbol;
class myAliasTemplate( alias someThing = defaultSymbol )
{
//the generic (non-specialised case)
}
//specialised version for only someParticularSymbol;
class myAliasTemplate( alias someThing: someParticularSymbol)
{
//the specialised case)
}
//=======================================
Another approach:
//=======================================
class myAliasTemplate( alias someThing = defaultSymbol )
{
static if( is(typeof(someThing)==int) )
{
//Define contents for int (specialised)
}
else
{
//Define contents otherwise (generic)
}
}
//=======================================
I'm using DMD v1.036 on linux.
Paul Thompson
(p dot thompson at acfr dot usyd dot edu dot au )
More information about the Digitalmars-d
mailing list