"else if" for template constraints

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Wed Aug 19 07:34:40 PDT 2015


On 8/17/15 6:44 PM, anonymous wrote:
> 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)) {
>          ...
>      }
> }
>

What if there is another foo template that handles double? possibly in 
another file? There would be a conflict for instantiation.

That is why we use template constraints instead of static if -- the 
constraint disqualifies the instantiation.

-Steve


More information about the Digitalmars-d mailing list