Thread overview | ||||||
---|---|---|---|---|---|---|
|
May 24, 2020 Static assert triggered in struct constructor that shouldn't be called | ||||
---|---|---|---|---|
| ||||
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; } |
May 24, 2020 Re: Static assert triggered in struct constructor that shouldn't be called | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | On Sun, May 24, 2020 at 09:34:53PM +0000, jmh530 via Digitalmars-d-learn wrote: > 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. [...] The problem is that static assert triggers when the function is compiled (not when it's called), and since your ctor is not a template function, it will always be compiled. Hence the static assert will always trigger. T -- The trouble with TCP jokes is that it's like hearing the same joke over and over. |
May 24, 2020 Re: Static assert triggered in struct constructor that shouldn't be called | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | On Sunday, 24 May 2020 at 21:34:53 UTC, jmh530 wrote:
> 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.
static assert is triggered when the code is *compiled*, whether it is actually run or not.
So as long s the function exists, its static asserts must all pass before it even gets to the runtime decision of if it is actually called or no.
|
May 24, 2020 Re: Static assert triggered in struct constructor that shouldn't be called | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Sunday, 24 May 2020 at 21:43:34 UTC, H. S. Teoh wrote:
> On Sun, May 24, 2020 at 09:34:53PM +0000, jmh530 via Digitalmars-d-learn wrote:
>> 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.
> [...]
>
> The problem is that static assert triggers when the function is compiled (not when it's called), and since your ctor is not a template function, it will always be compiled. Hence the static assert will always trigger.
>
>
> T
Thanks. Makes sense.
|
Copyright © 1999-2021 by the D Language Foundation