static assert(0) in template is a disaster

Avrina avrina12309412342 at gmail.com
Fri Jun 19 15:11:29 UTC 2020


On Friday, 19 June 2020 at 12:38:44 UTC, Stefan Koch wrote:
> 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.

Not sure what you mean, it's wrong, just plain wrong, it doesn't 
matter if it's more or less wrong, it is still wrong.

Like I said, if you use Foo.sizeof, you get a compiler error. 
That's what it should be, a compiler error because it is 
ill-formed. It's wrong for the same reason this is wrong:


struct Foo {
     typeof(Foo.a) a;
}

It doesn't make sense. The compiler just allows incorrect logic. 
There's nothing to formalize to make it work in a way that makes 
sense.


More information about the Digitalmars-d mailing list