No shortcircuit for static if or template constraints?

stewart via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Oct 24 17:02:57 PDT 2015


On Saturday, 24 October 2015 at 23:59:02 UTC, qsdfghjk wrote:
> On Saturday, 24 October 2015 at 23:34:19 UTC, stewart wrote:
>> On Saturday, 24 October 2015 at 23:26:09 UTC, stewart wrote:
>>> [...]
>>
>>
>> Oh and the workaround I'm using is this:
>>
>> ---
>> void func(T)(T vals) {
>>     static if(isInputRange!T) {
>>         static if(isIntegral!(ForeachType!T)) {
>>             // Do something with range
>>         }
>>     } else {
>>         // do something with scalar
>>     }
>> }
>> ---
>>
>> which is a bit ugly.
>
> Maybe this could work:
>
> ---
> enum isSupportedRange(T) = isInputRange!T && is(ForeachType!T) 
> && (isIntegral!(ElementType!T));
> ---
>
> ElementType() should return exactly what you excpeted with 
> ForeachType().

Yep, that works, thanks!


I also found I can do it with __traits, but I think your way is 
cleaner.

enum bool isSupportedRange(T) = __traits(compiles, isInputRange!T 
&& isIntegral!(ForeachType!T));

cheers,
stew



More information about the Digitalmars-d-learn mailing list