Static assert triggered in struct constructor that shouldn't be called

jmh530 john.michael.hall at gmail.com
Sun May 24 21:34:53 UTC 2020


The following code results in the static assert in the 
constructor being triggered, even though I would have thought no 
constructor would have been called. I know that there is an easy 
fix for this (move the static if outside the constructor), but it 
still seems like it doesn't make sense.

enum Foo
{
     A,
     B,
}

struct Bar(Foo foo)
{
     static if (foo == Foo.A)
     {
         float x = 0.5;
         long y = 1;
     }
     else static if (foo == Foo.B)
     {
         int p = 1;
     }

     this(long exp, float x)
     {
         static if (foo == Foo.A) {
             this.y = exp;
             this.x = x;
         } else {
             static assert(0, "Not implemented");
         }
     }
}

void main()
{
     Bar!(Foo.B) x;
}


More information about the Digitalmars-d-learn mailing list