C++ conversions

Marc Glisse marc.glisse at inria.fr
Fri May 11 21:28:40 CEST 2012


Hello,

currently, conversions from builtin types to mp*_class are implicit. And 
to convert in the opposite direction, we need to use member functions like 
get_si(), get_ui(), get_d(), and we can check beforehand if the conversion 
is safe using fits_*_p().

In C++11, there are explicit conversions. This means that we could allow 
converting an mpz_class to an int when the conversion is explicit (say 
with static_cast) without allowing it to happen implicitly (if a function 
takes an int and you pass it an mpz_class, it won't compile). This seems 
like a nice feature to have. Would people want it?

The behavior of the get_* functions is to assume that fits_*_p returned 
true and proceed to return whatever. Do we want the same behavior for the 
explicit casts, or do we want to integrate checking into them and throw if 
the numbers don't fit?

Note that there is no particular hurry, so we could wait and see if the 
discussions on a bigint type in C++1y give good arguments for one 
alternative.

I already added explicit conversion to bool, so one can use an mpz_class z 
in expressions like:
if(z)
if(!z)
z && something

This makes the gmp types behave a bit more like builtin types.

-- 
Marc Glisse


More information about the gmp-discuss mailing list