is(typeof(...)) vs __traits(compiles, ...)

drug drug2004 at bk.ru
Fri Feb 21 10:15:03 UTC 2020


Currently this code does not compiles:
```
unittest
{
     class MyClass
     {
         T opCall(T)(T p)
         {
             return p;
         }
     }

     import std.container.array : Array;

     Array!MyClass arr;
}
```
but if you comment out `opCall` in MyClass this code compiles. This is 
caused by this in std.conv(4434):
```
	static if (is(typeof(chunk = T(args))))
                 chunk = T(args);
```
The reason is that `is(typeof(chunk = T(args)))` returns true but does 
not compiles becase MyClass has `opCall`, compiler calls `opCall` but it 
needs `this` pointer that is unavailable. I replaced it by
```
	static if (__traits(compiles, chunk = T(args)))
                 chunk = T(args);
```
it works but I'm not sure this good solution. The question is - 
shouldn't `typeof` return false in this case? if so then the right fix 
would be fix typeof.


More information about the Digitalmars-d-learn mailing list