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

rikki cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 16 04:22:02 PDT 2016


On 16/03/16 11:18 PM, 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"
> }
>
> 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

Change those static if's to just plain old ifs.

Imagine those foreach loops being unrolled and what that happens there 
isn't just one return there, imagine many many many of them and all of 
them valid.

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus



More information about the Digitalmars-d-learn mailing list