On Monday, 17 August 2015 at 22:44:15 UTC, 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)) {
        ...
    }
}

There is also, as a similar option, the "dispatcher" solution, like for 'std.conv.to'

There is the main template that dispatches the call to the right non templated (or specialized) overload, so that the entry point is just used to redirect the cases that are fundamentaly different.

---
auto foo(T)
{
    static if(...) return fooImpl!T(); else
    static if(...) return fooImpl!T(); else
    //etc
}

private auto fooImpl(T)(){}
private auto fooImpl(T)(){}
private auto fooImpl(T)(){}
---


Reply via email to