Default template arguments

bearophile bearophileHUGS at lycos.com
Mon Nov 19 09:44:30 PST 2012


Better:

import std.stdio;

private void printSizeHelper(T)(T x) {
     writeln(T.stringof);
}

void printSize(T1 = void, T2)(T2 x) {
     static if (is(T1 == void))
         printSizeHelper!real(x);
     else
         printSizeHelper!T1(x);
}

void main() {
     float x;
     x.printSize();
     x.printSize!float();
     x.printSize!real();
}


(Keeping the two functions separated is useful against template 
bloat.)

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list