C++ conversions

Hans Aberg haberg-1 at telia.com
Fri May 11 22:30:18 CEST 2012

On 11 May 2012, at 21:28, Marc Glisse wrote:

> 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?

Marking them 'explicit' I think gets rid of the kind of type conversion conflicts I saw in pre-C++11. So add them.

> 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?

One possibility is throwing exceptions on type casts, and adding other functions without them called say get_<type>(). The rationale is that these types usually are used to avoid overflows, but still allowing to avoid the overhead if a truncation is needed.

Also, perhaps there should be conversions for the types uint32_t, etc.


More information about the gmp-discuss mailing list