gmpxx implicit conversion between types

Marc Glisse marc.glisse at
Sat Mar 5 21:20:38 CET 2011


currently, gmpxx has implicit conversions between all types. For instance, 
if I have:
void f(mpz_class);
void f(mpq_class);
mpq_class q;

this last call is ambiguous as q+q can just as well be converted to a 
mpz_class as a mpq_class. Without the second line, q+q would silently be 
converted to an integer, which I think is scary.

One thing that would be possible is making it so that this conversion is 
only possible if explicitly asked for, like mpz_class(q+q), but doesn't 
happen implicitly in f(q+q). It wouldn't change the fact that any integral 
expression can implicitly be converted to a rational, which is ok.

More generally, we would need to decide which conversions should be 
implicit and which explicit between mpz_class, mpq_class and mpf_class (I 
never use that last one which is why I am not using it as an example).

In case you are interested, I hit this while trying to make a generic 
fraction creator that takes two numbers n and d and returns a fraction 
n/d. If n or d is already a fraction, I just do a division. If n and d are 
both convertible to a numerator (resp. denominator) type, then I compose 
them. This convertibility test fails with gmpxx.

Marc Glisse

More information about the gmp-discuss mailing list