Fixing spurious "statement is not reachable" in template code

Daniel Murphy via Digitalmars-d digitalmars-d at puremagic.com
Tue Oct 27 00:50:38 PDT 2015


On 25/10/2015 4:25 AM, tsbockman wrote:
> ///////////////////////////////
> module main;
>
> import std.stdio;
>
> void reachIf(bool x)()
> {
>      if(!x)
>          return;
>      writeln("reached"); // Warning: statement is not reachable
> }
>
> void main(string[] args) {
>      reachIf!true();  // prints "reached"
>      reachIf!false(); // triggers warning
> }
> ///////////////////////////////
>


>
> Thoughts?

Easy to fix:

void reachIf(bool x)()
{
      static if(!x)
          return;
      else
          writeln("reached");
}

The warning is correct, and incredibly annoying.



More information about the Digitalmars-d mailing list