__traits(compiles) is true with warnings as errors

Jonathan Levi catanscout at gmail.com
Wed Oct 13 17:58:23 UTC 2021


When dmd is passed the "-w" tag, it "treats warnings as errors" 
but not with the "__traits(compiles)" expression.  Is this the 
intended action?

This code should compile even with "-w", but it does not.

     int i;
     static if (__traits(compiles,i += 5.2))
     	i += 5.2;


`pragma(msg, __traits(compiles,i += 5.2))` is `true`, even though 
it will stop the compiler.

My specific use case, where I am hindered by this, is with a 
guarded template function akin to this:

     void fun(T,U)(ref T a, const U b)
     if (__traits(compiles, a += b))
     {
         a += b;
     }

I think this is a bug.  The Docs for __traits compiles state 
"Returns a bool true if all of the arguments compile . . ." which 
is not holding true.


More information about the Digitalmars-d-learn mailing list