[PPL-devel] Re: Even more questionable feature of the C++ interface (GMP 4.0.1)

Ballabio Gerardo - Dip. di Scienza dei Materiali gerardo.ballabio at unimib.it
Fri Apr 5 16:20:37 CEST 2002


On 2002.04.02 00:45 Kevin Ryde wrote:
> I'm not sure why there's a constructor but not an assignment for an
> mpz_t.  There's probably a good reason.  Gerardo?
> 
> But yes, in any case we should drop the bool forms to avoid such
> subtle problems.

> > My revised proposal is that these two lines in <gmpxx.hh>
> >
> >      __gmp_expr(bool b) { mpz_init_set_ui(mp, b); }
> >      __gmp_expr & operator=(bool b) { mpz_set_ui(mp, b); return
> *this; }
> >
> > are _replaced_ by their analagous taking an mpz_t argument.

Sorry for my slow response...

I didn't know about automatic conversion from pointers to bool type. I
guess it's for fast testing whether a pointer is null.
In view of that, I agree now that construction and assignment from
bool must be removed. They're just too dangerous.

Instead, I'm not convinced that assignment from mpz_srcptr should be
provided. If so, then one could expect other operators as well -- for
example, mpz_class + mpz_t, and so on.
I don't think we should want too much interoperability between
mpz_class and mpz_t. In my opinion, the right way is "if you use C++,
then use mpz_class, not mpz_t" except for backward compatibility or
using GMP functions not (yet) supported by the C++ interface. And even
in that case, the correct way should be

  mpz_class z, w;
  ...
  mpz_something(z.get_mpz_t(), w.get_mpz_t());

rather than

  mpz_class z;
  mpz_t w, v;
  ...
  mpz_something(w, v);
  z = w;

Therefore, my approach has been that one should never operate on mpz_t
without _explicitly_ promoting it to mpz_class. That's why I defined
an explicit constructor as the _only_ way to do the conversion.

Given that, we might not want to define a constructor either,
replacing it with an mpz_class::set_mpz_t(mpz_srcptr) function.
However, I'm not willing to give up the commodity of being able to
write things like

  z = mpz_class(w) + 1; // z is mpz_class, w is mpz_t

In attachment there's a version of gmpxx.h where I removed all
constructors and assignments from bool -- but I didn't add their mpz_t
counterparts.
In passing, I've also removed mpf_class::set_str2() (I think now that
mpf_class::set_str() is good enough), and thus the need to #include
the huge <iostream> (<iosfwd> is enough) and <strstream>.

Gerardo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: gmpxx.h
Type: text/x-c
Size: 180391 bytes
Desc: not available
URL: <http://www.cs.unipr.it/pipermail/ppl-devel/attachments/20020405/d567a654/attachment.bin>


More information about the PPL-devel mailing list