[Gmp-commit] /var/hg/gmp: Warn about C++11 auto. The second example can be up...

mercurial at gmplib.org mercurial at gmplib.org
Sat Jan 18 16:41:52 UTC 2014


details:   /var/hg/gmp/rev/4f0bb1002c5c
changeset: 16199:4f0bb1002c5c
user:      Marc Glisse <marc.glisse at inria.fr>
date:      Sat Jan 18 17:41:28 2014 +0100
description:
Warn about C++11 auto. The second example can be updated if we make it valid later.

diffstat:

 ChangeLog    |   4 ++++
 doc/gmp.texi |  25 +++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 0 deletions(-)

diffs (46 lines):

diff -r f1ee9ed6f1d2 -r 4f0bb1002c5c ChangeLog
--- a/ChangeLog	Sat Jan 18 17:06:06 2014 +0100
+++ b/ChangeLog	Sat Jan 18 17:41:28 2014 +0100
@@ -1,3 +1,7 @@
+2014-01-18  Marc Glisse  <marc.glisse at inria.fr>
+
+	* doc/gmp.texi (C++ Interface Limitations): Warn against C++11 auto.
+
 2014-01-18 Marco Bodrato <bodrato at mail.dm.unipi.it>
 
 	* tests/t-parity.c: Use 1UL to generate unsigned constants.
diff -r f1ee9ed6f1d2 -r 4f0bb1002c5c doc/gmp.texi
--- a/doc/gmp.texi	Sat Jan 18 17:06:06 2014 +0100
+++ b/doc/gmp.texi	Sat Jan 18 17:41:28 2014 +0100
@@ -7271,6 +7271,31 @@
   fun2 (f, T(f+g));  // Good
 @}
 @end example
+
+ at item C++11
+C++11 provides several new ways in which types can be inferred: @code{auto},
+ at code{decltype}, etc. While they can be very convenient, they don't mix well
+with expression templates. In this example, the addition is performed twice,
+as if we had defined @code{sum} as a macro.
+
+ at example
+mpz_class z = 33;
+auto sum = z + z;
+mpz_class prod = sum * sum;
+ at end example
+
+This other example may crash, though some compilers might make it look like
+it is working, because the expression @code{z+z} goes out of scope before it
+is evaluated.
+
+ at example
+mpz_class z = 33;
+auto sum = z + z + z;
+mpz_class prod = sum * 2;
+ at end example
+
+It is thus strongly recommended to avoid @code{auto} anywhere a GMP C++
+expression may appear.
 @end table
 
 


More information about the gmp-commit mailing list