"else if" for template constraints

anonymous via Digitalmars-d digitalmars-d at puremagic.com
Mon Aug 17 15:44:13 PDT 2015


On Monday, 17 August 2015 at 22:32:10 UTC, Idan Arye wrote:
> On Monday, 17 August 2015 at 21:27:47 UTC, Meta wrote:
[...]
>> At that point, couldn't you just use static if inside the body 
>> of the template instead of using template constraints?
>
> No. Consider this: http://dpaste.dzfl.pl/a014aeba6e68. The 
> having two foo templates is illegal(though it'll only show when 
> you try to instantiate foo), because each of them covers all 
> options for T. When T is neither int nor float, the foo 
> *function* in the first template is not defined, but the *foo* 
> template is still there.

The idea is to have only one template:

template foo(T) {
     static if (is(T == int)) {
         ...
     } else static if (is(T == float)) {
         ...
     } else static if (is(T == char)) {
         ...
     } else static if (is(T == bool)) {
         ...
     }
}



More information about the Digitalmars-d mailing list