Static ternary if

Michael Coulombe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 25 17:54:59 PDT 2016


On Monday, 25 July 2016 at 22:57:05 UTC, Gorge Jingale wrote:
> On Monday, 25 July 2016 at 22:27:11 UTC, Cauterite wrote:
>> On Monday, 25 July 2016 at 02:15:12 UTC, Gorge Jingale wrote:
>>> Is there a static ternary if?
>>>
>>> (A == B) ? C : D;
>>>
>>> for compile type that works like static if.
>>
>> You can pretty easily make your own;
>>
>>   template staticIf(bool cond, alias a, alias b) {
>>     static if (cond) {
>>       alias staticIf = a;
>>     } else {
>>       alias staticIf = b;
>>     };
>>   };
>>
>> The drawback is that there's no 'short-circuiting'; a and b 
>> are both evaluated.
>
> Cool, that would work. I don't think the lazy evaluation is a 
> problem at compile time? Just makes for longer times, but 
> should be pretty minuscule.

If that's ok, then try out std.traits.Select or std.traits.select:
https://dlang.org/phobos/std_traits.html#Select


More information about the Digitalmars-d-learn mailing list