[Gmp-commit] /var/hg/gmp: Make some conversions between C++ types explicit.

mercurial at gmplib.org mercurial at gmplib.org
Fri May 6 23:31:12 CEST 2011


details:   /var/hg/gmp/rev/4db44a7c9293
changeset: 14170:4db44a7c9293
user:      Marc Glisse <marc.glisse at inria.fr>
date:      Fri May 06 23:31:07 2011 +0200
description:
Make some conversions between C++ types explicit.

diffstat:

 ChangeLog             |   9 +++++
 NEWS                  |  11 ++++++
 doc/gmp.texi          |  13 ++++---
 gmpxx.h               |  13 ++++++-
 tests/cxx/Makefile.am |   3 +-
 tests/cxx/t-mix.cc    |  83 +++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 124 insertions(+), 8 deletions(-)

diffs (206 lines):

diff -r 68820ed796d9 -r 4db44a7c9293 ChangeLog
--- a/ChangeLog	Thu May 05 22:51:45 2011 +0200
+++ b/ChangeLog	Fri May 06 23:31:07 2011 +0200
@@ -1,3 +1,12 @@
+2011-05-06  Marc Glisse  <marc.glisse at inria.fr>
+
+	* gmpxx.h (mpz_class): Make constructor from mp[qf]_class explicit.
+	(mpq_class): Make constructor from mpf_class explicit.
+	* doc/gmp.texi: Document the above.
+	* NEWS: Likewise, and mention the EOF istream fix.
+	* tests/cxx/t-mix.cc: New file.
+	* tests/cxx/Makefile.am: Added t-mix.
+
 2011-05-05  Torbjorn Granlund  <tege at gmplib.org>
 
 	* mpz/gcdext.c: Correct sgn computation.
diff -r 68820ed796d9 -r 4db44a7c9293 NEWS
--- a/NEWS	Thu May 05 22:51:45 2011 +0200
+++ b/NEWS	Fri May 06 23:31:07 2011 +0200
@@ -5,6 +5,17 @@
 in any medium, provided this notice is preserved.
 
 
+Changes between GMP version 5.0.* and 5.1.0
+
+  BUGS FIXED
+  * When reading a C++ number (like mpz_class) in an istream reaches the end
+    of the stream, the eofbit is now set.
+
+  MISC
+  * In C++, the conversions mpq_class->mpz_class, mpf_class->mpz_class and
+    mpf_class->mpq_class are now explicit.
+
+
 Changes between GMP version 5.0.0 and 5.0.1
 
   BUGS FIXED
diff -r 68820ed796d9 -r 4db44a7c9293 doc/gmp.texi
--- a/doc/gmp.texi	Thu May 05 22:51:45 2011 +0200
+++ b/doc/gmp.texi	Fri May 06 23:31:07 2011 +0200
@@ -6499,8 +6499,10 @@
 @deftypefun {} mpz_class::mpz_class (type @var{n})
 Construct an @code{mpz_class}.  All the standard C++ types may be used, except
 @code{long long} and @code{long double}, and all the GMP C++ classes can be
-used.  Any necessary conversion follows the corresponding C function, for
-example @code{double} follows @code{mpz_set_d} (@pxref{Assigning Integers}).
+used, although conversions from @code{mpq_class} and @code{mpf_class} are
+ at code{explicit}.  Any necessary conversion follows the corresponding C
+function, for example @code{double} follows @code{mpz_set_d}
+(@pxref{Assigning Integers}).
 @end deftypefun
 
 @deftypefun explicit mpz_class::mpz_class (mpz_t @var{z})
@@ -6583,9 +6585,10 @@
 @deftypefun {} mpq_class::mpq_class (type @var{op})
 @deftypefunx {} mpq_class::mpq_class (integer @var{num}, integer @var{den})
 Construct an @code{mpq_class}.  The initial value can be a single value of any
-type, or a pair of integers (@code{mpz_class} or standard C++ integer types)
-representing a fraction, except that @code{long long} and @code{long double}
-are not supported.  For example,
+type (conversion from @code{mpf_class} is @code{explicit}), or a pair of
+integers (@code{mpz_class} or standard C++ integer types) representing a
+fraction, except that @code{long long} and @code{long double} are not
+supported.  For example,
 
 @example
 mpq_class q (99);
diff -r 68820ed796d9 -r 4db44a7c9293 gmpxx.h
--- a/gmpxx.h	Thu May 05 22:51:45 2011 +0200
+++ b/gmpxx.h	Fri May 06 23:31:07 2011 +0200
@@ -1288,8 +1288,11 @@
   __gmp_expr() { mpz_init(mp); }
 
   __gmp_expr(const __gmp_expr &z) { mpz_init_set(mp, z.mp); }
+  template <class T>
+  __gmp_expr(const __gmp_expr<mpz_t, T> &expr)
+  { mpz_init(mp); __gmp_set_expr(mp, expr); }
   template <class T, class U>
-  __gmp_expr(const __gmp_expr<T, U> &expr)
+  explicit __gmp_expr(const __gmp_expr<T, U> &expr)
   { mpz_init(mp); __gmp_set_expr(mp, expr); }
 
   __gmp_expr(signed char c) { mpz_init_set_si(mp, c); }
@@ -1456,8 +1459,14 @@
   __gmp_expr() { mpq_init(mp); }
 
   __gmp_expr(const __gmp_expr &q) { mpq_init(mp); mpq_set(mp, q.mp); }
+  template <class T>
+  __gmp_expr(const __gmp_expr<mpz_t, T> &expr)
+  { mpq_init(mp); __gmp_set_expr(mp, expr); }
+  template <class T>
+  __gmp_expr(const __gmp_expr<mpq_t, T> &expr)
+  { mpq_init(mp); __gmp_set_expr(mp, expr); }
   template <class T, class U>
-  __gmp_expr(const __gmp_expr<T, U> &expr)
+  explicit __gmp_expr(const __gmp_expr<T, U> &expr)
   { mpq_init(mp); __gmp_set_expr(mp, expr); }
 
   __gmp_expr(signed char c) { mpq_init(mp); mpq_set_si(mp, c, 1); }
diff -r 68820ed796d9 -r 4db44a7c9293 tests/cxx/Makefile.am
--- a/tests/cxx/Makefile.am	Thu May 05 22:51:45 2011 +0200
+++ b/tests/cxx/Makefile.am	Fri May 06 23:31:07 2011 +0200
@@ -31,7 +31,7 @@
 
 if WANT_CXX
 check_PROGRAMS = t-assign t-binary t-cast t-constr t-headers t-istream \
-  t-locale t-misc t-ops t-ops2 t-ostream t-prec t-rand t-ternary t-unary
+  t-locale t-misc t-mix t-ops t-ops2 t-ostream t-prec t-rand t-ternary t-unary
 TESTS = $(check_PROGRAMS)
 endif
 
@@ -43,6 +43,7 @@
 t_istream_SOURCES = t-istream.cc
 t_locale_SOURCES  = t-locale.cc clocale.c
 t_misc_SOURCES    = t-misc.cc
+t_mix_SOURCES     = t-mix.cc
 t_ops_SOURCES     = t-ops.cc
 t_ops2_SOURCES    = t-ops2.cc
 t_ostream_SOURCES = t-ostream.cc
diff -r 68820ed796d9 -r 4db44a7c9293 tests/cxx/t-mix.cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/cxx/t-mix.cc	Fri May 06 23:31:07 2011 +0200
@@ -0,0 +1,83 @@
+/* Test legality of conversion between the different mp*_class
+
+Copyright 2011 Free Software Foundation, Inc.
+
+This file is part of the GNU MP Library.
+
+The GNU MP Library is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 3 of the License, or (at your
+option) any later version.
+
+The GNU MP Library is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
+License for more details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
+
+#include "config.h"
+
+#include "gmp.h"
+#include "gmpxx.h"
+#include "gmp-impl.h"
+#include "tests.h"
+
+int f_z  (mpz_class){return 0;}
+int f_q  (mpq_class){return 1;}
+int f_f  (mpf_class){return 2;}
+int f_zq (mpz_class){return 0;}
+int f_zq (mpq_class){return 1;}
+int f_zf (mpz_class){return 0;}
+int f_zf (mpf_class){return 2;}
+int f_qf (mpq_class){return 1;}
+int f_qf (mpf_class){return 2;}
+int f_zqf(mpz_class){return 0;}
+int f_zqf(mpq_class){return 1;}
+int f_zqf(mpf_class){return 2;}
+
+void
+check (void)
+{
+  mpz_class z=42;
+  mpq_class q=33;
+  mpf_class f=18;
+
+  ASSERT_ALWAYS(f_z  (z)==0); ASSERT_ALWAYS(f_z  (-z)==0);
+  ASSERT_ALWAYS(f_q  (z)==1); ASSERT_ALWAYS(f_q  (-z)==1);
+  ASSERT_ALWAYS(f_q  (q)==1); ASSERT_ALWAYS(f_q  (-q)==1);
+  ASSERT_ALWAYS(f_f  (z)==2); ASSERT_ALWAYS(f_f  (-z)==2);
+  ASSERT_ALWAYS(f_f  (q)==2); ASSERT_ALWAYS(f_f  (-q)==2);
+  ASSERT_ALWAYS(f_f  (f)==2); ASSERT_ALWAYS(f_f  (-f)==2);
+  ASSERT_ALWAYS(f_zq (z)==0);
+  ASSERT_ALWAYS(f_zq (q)==1); ASSERT_ALWAYS(f_zq (-q)==1);
+  ASSERT_ALWAYS(f_zf (z)==0);
+  ASSERT_ALWAYS(f_zf (f)==2); ASSERT_ALWAYS(f_zf (-f)==2);
+  ASSERT_ALWAYS(f_qf (q)==1);
+  ASSERT_ALWAYS(f_qf (f)==2); ASSERT_ALWAYS(f_qf (-f)==2);
+  ASSERT_ALWAYS(f_zqf(z)==0);
+  ASSERT_ALWAYS(f_zqf(q)==1);
+  ASSERT_ALWAYS(f_zqf(f)==2); ASSERT_ALWAYS(f_zqf(-f)==2);
+
+  ASSERT_ALWAYS(f_zqf(mpz_class(z))==0); ASSERT_ALWAYS(f_zqf(mpz_class(-z))==0);
+  ASSERT_ALWAYS(f_zqf(mpz_class(q))==0); ASSERT_ALWAYS(f_zqf(mpz_class(-q))==0);
+  ASSERT_ALWAYS(f_zqf(mpz_class(f))==0); ASSERT_ALWAYS(f_zqf(mpz_class(-f))==0);
+  ASSERT_ALWAYS(f_zqf(mpq_class(z))==1); ASSERT_ALWAYS(f_zqf(mpq_class(-z))==1);
+  ASSERT_ALWAYS(f_zqf(mpq_class(q))==1); ASSERT_ALWAYS(f_zqf(mpq_class(-q))==1);
+  ASSERT_ALWAYS(f_zqf(mpq_class(f))==1); ASSERT_ALWAYS(f_zqf(mpq_class(-f))==1);
+  ASSERT_ALWAYS(f_zqf(mpf_class(z))==2); ASSERT_ALWAYS(f_zqf(mpf_class(-z))==2);
+  ASSERT_ALWAYS(f_zqf(mpf_class(q))==2); ASSERT_ALWAYS(f_zqf(mpf_class(-q))==2);
+  ASSERT_ALWAYS(f_zqf(mpf_class(f))==2); ASSERT_ALWAYS(f_zqf(mpf_class(-f))==2);
+}
+
+int
+main (void)
+{
+  tests_start();
+
+  check();
+
+  tests_end();
+  return 0;
+}


More information about the gmp-commit mailing list