Widening a type
bearophile
bearophileHUGS at lycos.com
Fri Apr 17 04:48:35 PDT 2009
Doctor J:
> Given a type parameter T of a template:
> If T is an integral type, I want to declare a variable 'widest' of type ulong;
> If T is a floating-point type, I want to declare a variable 'widest' of type double.
> And it has to be prettier than my solution. :)
A solution using my dlibs:
import std.stdio: writefln;
import d.templates: IsIntegral, IsType, If;
template Widened(T) {
alias If!(IsIntegral!(T), ulong, If!(IsType!(T, float, double, real), double, void)) Widened;
}
void main() {
writefln(typeid(Widened!(int))); // prints: ulong
writefln(typeid(Widened!(byte))); // prints: ulong
writefln(typeid(Widened!(real))); // prints: double
writefln(typeid(Widened!(float))); // prints: double
writefln(typeid(Widened!(double))); // prints: double
writefln(typeid(Widened!(char))); // prints: void
writefln(typeid(Widened!(string))); // prints: void
}
Note that real=>double and char=>void.
If you want char=>ulong, then you have to use another isType!(T, ...) instead of IsIntegral!(T).
dlibs (soon to be updated again to improve the apply()):
http://www.fantascienza.net/leonardo/so/libs_d.zip
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list