Question on Template Specializing.
evilrat
evilrat666 at gmail.com
Tue Aug 6 22:45:16 PDT 2013
On Wednesday, 7 August 2013 at 05:10:25 UTC, SteveGuo wrote:
> But my question is:
> If I am a library author, I write templates for my users, so
> users can specialize the template with any types they defined.
> I don't know what types they may define when I design the
> template, so how can I specialize the template for them in my
> template module?
if you want to make some presets for common types you can use
aliases
alias A!int Aint;
alias A!float Afloat;
...
if you want to enforce template accept only certain types you
should use static asserts or type covariance(oh sorry it's not
called this but i'm somehow stupid now)
-------------
// type limitation with overloading
interface Something { ... }
// specialized template for covariants of Something(will be
instantiated with any covariant of Something)
class A(T: Something) {
...
}
// generic template(instantiated when no specialized overloads
exists)
class A(T) {
}
-------------
or static asserts/if
-------------
import std.traits;
class A(T) {
// code for numeric types
static if(isNumeric(T)) {
...
}
// code for callable objects
static if(isCallable(T)) {
}
// and so on...
}
More information about the Digitalmars-d
mailing list