static assert not printing out along the error diagnostic

SealabJaster sealabjaster at gmail.com
Wed Jul 14 05:59:19 UTC 2021


On Wednesday, 14 July 2021 at 03:06:06 UTC, someone wrote:
> ...

I've ran into this rarely. At a guess, it seems something 
triggers the compiler to either evaluate something in non-lexical 
order; doesn't realise it needs to error out at the static assert 
instead of doing it later, or maybe it somehow ends up gagging 
the more relevant static assert error.

The fix usually involves changing the code in a way to make the 
compiler evaluate things differently.

For your example:

```d
import std.stdio;

struct MyType(T) {

     template ResultTypeT()
     {
         static if (is (T == float)) {
             alias ResultTypeT = double;

         } else static if (is (T == double)) {
             alias ResultTypeT = real;

         } else {
             static assert(false, T.stringof ~ " is not 
supported");
         }
     }
     alias ResultType = ResultTypeT!();

     ResultType doWork() {
         writefln("The return type for %s is %s.",
                  T.stringof, ResultType.stringof);
         ResultType result;
         // ...
         return result;
     }
}

void main() {
     auto f = MyType!float();
     f.doWork();

     auto d = MyType!double();
     d.doWork();

     auto g = MyType!string();
     g.doWork();
}
```


More information about the Digitalmars-d-learn mailing list