Function hijack on selective import

rumbu rumbu at rumbu.ro
Tue Dec 26 19:41:47 UTC 2017


On Tuesday, 26 December 2017 at 16:15:55 UTC, Adam D. Ruppe wrote:
> The mistake you're making is using a constraint when you should 
> try a specialization:
>
>
> int signbit(T:Custom)(T x)
> {
>     return 0;
> }
>
>
> That means to use this specialized function when T is Custom. 
> Now, you just need to merge the overload sets:
>
> import std.math;
> alias signbit = std.math.signbit; // this merges local signbit 
> with std.math.signbit
>
>
> and boom it should compile and call your version.

"Custom" is a templated struct. I cannot imagine all the 
instantiations of Custom to write template specialisations for 
each of them.

My opinion is that the mistake is in std.math, because 
std.math.signbit accepts any type instead to be constrained to 
float types. Actually, calling std.math.signbit with any other 
type than float will result in compiler error because signbit 
expects some float traits for the provided type:

int signbit(X)(X x) @nogc @trusted pure nothrow
{
     alias F = floatTraits!(X);
     return ((cast(ubyte *)&x)[F.SIGNPOS_BYTE] & 0x80) != 0;
}




More information about the Digitalmars-d-learn mailing list