On Tuesday, 13 March 2018 at 14:40:21 UTC, Steven Schveighoffer wrote:
On 3/13/18 10:25 AM, Yuxuan Shui wrote:
[...]

This has been discussed before. There are a few ways around this. One is to do what you did. Another is to append a sentinel, or use id to terminate the loop:

foreach(id, R; S) {
    static if(is(T == R))
        return id;
    else static if(id + 1 == S.length)
        return -1;
}

Wait, I don't understand how this works.

Isn't this going to be expanded to something like:

return 0;
return 4;
// One return for every match
...
return -1;

Shouldn't this trigger unreachable warning too?


IMO, the "unreachable statement" error is bogus because it's reachable depending on the template parameters! In the coder's eyes, what matters is whether the line of source is reachable or not, not whether it's reachable in that instantiation.

Agreed.


-Steve

Reply via email to