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 10:35:53 PDT 2016


On Wednesday, 16 March 2016 at 17:34:13 UTC, Steven Schveighoffer 
wrote:
> 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;
>    }
> }

I like your second solution, it's clever :)




More information about the Digitalmars-d-learn mailing list