[Gmp-commit] /var/hg/gmp: explicit conversion to bool

mercurial at gmplib.org mercurial at gmplib.org
Fri May 11 19:51:42 CEST 2012


details:   /var/hg/gmp/rev/96ad88f37fa2
changeset: 14964:96ad88f37fa2
user:      Marc Glisse <marc.glisse at inria.fr>
date:      Fri May 11 19:51:40 2012 +0200
description:
explicit conversion to bool

diffstat:

 ChangeLog            |   5 +++++
 gmpxx.h              |  12 ++++++++++++
 tests/cxx/t-cxx11.cc |  29 ++++++++++++++++++++++++++++-
 3 files changed, 45 insertions(+), 1 deletions(-)

diffs (100 lines):

diff -r 240c6d87f9d2 -r 96ad88f37fa2 ChangeLog
--- a/ChangeLog	Thu May 10 18:30:22 2012 +0200
+++ b/ChangeLog	Fri May 11 19:51:40 2012 +0200
@@ -1,3 +1,8 @@
+2012-05-11  Marc Glisse  <marc.glisse at inria.fr>
+
+	* gmpxx.h (explicit operator bool): New functions.
+	* tests/cxx/t-cxx11.cc: Test the above.
+
 2012-05-10 Marco Bodrato <bodrato at mail.dm.unipi.it>
 
 	* gmp-impl.h (__gmpn_cpuvec_initialized): Was __gmpn_cpuvec.initialized
diff -r 240c6d87f9d2 -r 96ad88f37fa2 gmpxx.h
--- a/gmpxx.h	Thu May 10 18:30:22 2012 +0200
+++ b/gmpxx.h	Fri May 11 19:51:40 2012 +0200
@@ -1563,6 +1563,10 @@
   // bool fits_double_p() const { return mpz_fits_double_p(mp); }
   // bool fits_ldouble_p() const { return mpz_fits_ldouble_p(mp); }
 
+#if __GMPXX_USE_CXX11
+  explicit operator bool() const { return mp->_mp_size != 0; }
+#endif
+
   // member operators
   __GMP_DECLARE_COMPOUND_OPERATOR(operator+=)
   __GMP_DECLARE_COMPOUND_OPERATOR(operator-=)
@@ -1755,6 +1759,10 @@
 
   double get_d() const { return mpq_get_d(mp); }
 
+#if __GMPXX_USE_CXX11
+  explicit operator bool() const { return mpq_numref(mp)->_mp_size != 0; }
+#endif
+
   // compound assignments
   __GMP_DECLARE_COMPOUND_OPERATOR(operator+=)
   __GMP_DECLARE_COMPOUND_OPERATOR(operator-=)
@@ -1963,6 +1971,10 @@
   // bool fits_double_p() const { return mpf_fits_double_p(mp); }
   // bool fits_ldouble_p() const { return mpf_fits_ldouble_p(mp); }
 
+#if __GMPXX_USE_CXX11
+  explicit operator bool() const { return mp->_mp_size != 0; }
+#endif
+
   // compound assignments
   __GMP_DECLARE_COMPOUND_OPERATOR(operator+=)
   __GMP_DECLARE_COMPOUND_OPERATOR(operator-=)
diff -r 240c6d87f9d2 -r 96ad88f37fa2 tests/cxx/t-cxx11.cc
--- a/tests/cxx/t-cxx11.cc	Thu May 10 18:30:22 2012 +0200
+++ b/tests/cxx/t-cxx11.cc	Fri May 11 19:51:40 2012 +0200
@@ -1,6 +1,6 @@
 /* Test C++11 features
 
-Copyright 2011 Free Software Foundation, Inc.
+Copyright 2011, 2012 Free Software Foundation, Inc.
 
 This file is part of the GNU MP Library.
 
@@ -168,6 +168,32 @@
   }
 }
 
+// Check for explicit conversion to bool
+void implicit_bool(bool);
+int implicit_bool(...);
+
+void check_bool_conversion ()
+{
+  const mpz_class zn = -2;
+  const mpq_class qn = -2;
+  const mpf_class fn = -2;
+  const mpz_class z0 =  0;
+  const mpq_class q0 =  0;
+  const mpf_class f0 =  0;
+  const mpz_class zp = +2;
+  const mpq_class qp = +2;
+  const mpf_class fp = +2;
+  if (zn && qn && fn && zp && qp && fp && !z0 && !q0 && !f0)
+    {
+      if (z0 || q0 || f0) ASSERT_ALWAYS(false);
+    }
+  else ASSERT_ALWAYS(false);
+  decltype(implicit_bool(zn)) zi = 1;
+  decltype(implicit_bool(qn)) qi = 1;
+  decltype(implicit_bool(fn)) fi = 1;
+  (void)(zi+qi+fi);
+}
+
 int
 main (void)
 {
@@ -184,6 +210,7 @@
   check_move_init<mpz_class,mpq_class>();
   check_move_assign<mpz_class,mpq_class>();
   check_user_defined_literal();
+  check_bool_conversion();
 
   tests_end();
   return 0;


More information about the gmp-commit mailing list