detecting classes, structs, arrays in templates
Luke J. West
luke at west.me.uk
Sun Jan 23 05:46:51 PST 2011
Hi,
I want to specialize a template function - call it print() - for three
cases: classes, structs and arrays. Ideally I'd like something that
looks 'functional' like a proper specialization, but perhaps I need to
use "static if". I'm still at the beginning of my journey with D so I'd
be grateful for any pointers (sorry - that's a terrible pun). Perhaps
there's a corner of the D2 docs I've glossed over.
Thanks,
Luke
// in words you can understand...
void print(T)() {writefln("general");}
// But what does my specialization look like for all (or any)
// of the implementations of print() for the calls below.
int main() {
C c; // some class
S s; // some struct
int a[4]; // an array
int p; // a primitive type
print(c); // writefln("class");
print(s); // writefln("struct");
print(a); // writefln("array");
print(p); // writefln("primitive");
return 0;
}
More information about the Digitalmars-d
mailing list