static assert(0) in template is a disaster

Stefan Koch uplink.coder at googlemail.com
Fri Jun 19 12:38:44 UTC 2020


On Wednesday, 17 June 2020 at 16:08:41 UTC, Avrina wrote:
> On Wednesday, 17 June 2020 at 03:15:52 UTC, Stefan Koch wrote:
>> [...]
>
> It's illogical, the size of Foo changes because it is querying 
> it for information before the structure is even defined. Yes I 
> imagine there is probably a lot of code that compiles that 
> shouldn't. There's probably a lot of silent bugs, and from time 
> to time when D changes and the order the of the semantic 
> changes, code that once did compile won't compile anymore 
> because the code never made sense in the first place.
>
>
> static assert(Foo.tupleof.length == 1);
>
> struct Foo {
>  	int a;
>
>     static if(Foo.tupleof.length == 1) {
>     	int b;
>         static assert(Foo.tupleof.length == 3); // ok
>     }
>
>     static if(Foo.tupleof.length == 1) {
>     	int c;
>     }
>
>     static assert(Foo.tupleof.length == 3); // ok
>     static if(Foo.tupleof.length == 3) {
>     	int d;
>         static assert(0); // never run
>     }
> }

For all static if's the length to the tuple is 1.
For all static asserts the length of the tuple is 3.

You made it look more variant than it is.

What we have to do is to formalize the behavior.


More information about the Digitalmars-d mailing list