Lazy template arguments?

bearophile bearophileHUGS at lycos.com
Sat Jan 5 02:37:30 PST 2013


In some case I'd like a hypothetical static ternary operator 
usable on types:

alias T = (U.sizeof <= 16) ?? ushort : size_t;

That is equivalent to:

static if (U.sizeof <= 16) {
     alias T = ushort;
} else {
     alias T = size_t;
}


The syntax of a possible library implementation is acceptable:

alias T = Ternary!(U.sizeof <= 16, ushort, size_t);


But I think to implement it well in library code you need a kind 
of "lazy" for types (it means the T2 type is not computed if b is 
false):


template Ternary(bool b, T1, lazy T2) {
     static if (b)
         alias Ternary = T1;
     else
         alias Ternary = T2;
}


Are such lazy type arguments generally useful for other purposes?

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list