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

Johan Engelen via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 16 04:18:36 PDT 2016


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"
}

bool nobool_nowarning(T...)() {
     bool retval = true;
     foreach (i, U; T) {
         static if (is(U == bool)) {
             retval = false;
         }
     }
     return retval;
}

void main() {
     static assert ( nobool_nowarning!(int,bool)() == false );
     static assert ( nobool_nowarning!(int,int)() == true );

     static assert ( nobool!(int,bool)() == false );
     static assert ( nobool!(int,int)() == true );
}
```
(I have heavily simplified the real-world code, please don't 
discuss alternative solutions to the "is(U==bool)" in particular. 
For sake of argument, assume that the predicate is a complicated 
beast.)

The `nobool` template prevents compilation with `-w`. Is 
`nobool_nowarning`, with the early return eradicated, the only 
acceptable solution? (with the hope that there will not be a 
"dead store" warning in the future...)
What if early returns cannot be avoided?

Thanks,
   Johan



More information about the Digitalmars-d-learn mailing list