Solution to "statement is not reachable" depending on template variables?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 16 10:34:13 PDT 2016


On 3/16/16 7:18 AM, Johan Engelen wrote:
> Hi all,
>    I've found discussions, but not an actual "recommended" solution for
> the problem of "statement is not reachable" warnings in templates with
> early returns, e.g.:
> ```
> bool nobool(T...)() {
>      foreach (i, U; T) {
>          static if (is(U == bool)) {
>              return false;
>          }
>      }
>      return true;  // emits "Warning: statement is not reachable"
> }

Instead of foreach, you could use recursive mechanism. Not ideal, but it 
would work.

Another possibility:

foreach(i, U; T) {
    static if(is(U == bool)) {
        return false;
    } else static if(i + 1 == T.length) {
        return true;
    }
}

-Steve


More information about the Digitalmars-d-learn mailing list