Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r85418 - in trunk: boost libs/rational/test
From: dwalker07_at_[hidden]
Date: 2013-08-21 19:24:54


Author: dlwalker
Date: 2013-08-21 19:24:54 EDT (Wed, 21 Aug 2013)
New Revision: 85418
URL: http://svn.boost.org/trac/boost/changeset/85418

Log:
Used static_cast to suppress a warning/error about a narrowing conversion. Fixes #5855.

Text files modified:
   trunk/boost/rational.hpp | 12 +++++++-----
   trunk/libs/rational/test/rational_test.cpp | 33 +++++++++++++++++++++++++++++++++
   2 files changed, 40 insertions(+), 5 deletions(-)

Modified: trunk/boost/rational.hpp
==============================================================================
--- trunk/boost/rational.hpp Wed Aug 21 17:07:29 2013 (r85417)
+++ trunk/boost/rational.hpp 2013-08-21 19:24:54 EDT (Wed, 21 Aug 2013) (r85418)
@@ -29,9 +29,9 @@
 // 31 Oct 06 Recoded both operator< to use round-to-negative-infinity
 // divisions; the rational-value version now uses continued fraction
 // expansion to avoid overflows, for bug #798357 (Daryle Walker)
-// 20 Oct 06 Fix operator bool_type for CW 8.3 (Joaquín M López Muñoz)
+// 20 Oct 06 Fix operator bool_type for CW 8.3 (Joaquín M López Muñoz)
 // 18 Oct 06 Use EXPLICIT_TEMPLATE_TYPE helper macros from Boost.Config
-// (Joaquín M López Muñoz)
+// (Joaquín M López Muñoz)
 // 27 Dec 05 Add Boolean conversion operator (Daryle Walker)
 // 28 Sep 02 Use _left versions of operators from operators.hpp
 // 05 Jul 01 Recode gcd(), avoiding std::swap (Helmut Zeisel)
@@ -380,9 +380,11 @@
 
     // Determine relative order by expanding each value to its simple continued
     // fraction representation using the Euclidian GCD algorithm.
- struct { int_type n, d, q, r; } ts = { this->num, this->den, this->num /
- this->den, this->num % this->den }, rs = { r.num, r.den, r.num / r.den,
- r.num % r.den };
+ struct { int_type n, d, q, r; }
+ ts = { this->num, this->den, static_cast<int_type>(this->num / this->den),
+ static_cast<int_type>(this->num % this->den) },
+ rs = { r.num, r.den, static_cast<int_type>(r.num / r.den),
+ static_cast<int_type>(r.num % r.den) };
     unsigned reverse = 0u;
 
     // Normalize negative moduli by repeatedly adding the (positive) denominator

Modified: trunk/libs/rational/test/rational_test.cpp
==============================================================================
--- trunk/libs/rational/test/rational_test.cpp Wed Aug 21 17:07:29 2013 (r85417)
+++ trunk/libs/rational/test/rational_test.cpp 2013-08-21 19:24:54 EDT (Wed, 21 Aug 2013) (r85418)
@@ -979,4 +979,37 @@
 #endif
 }
 
+// The bug/patch numbers for the above 3 tests are from our SourceForge repo
+// before we moved to our own SVN & Trac server. At the time this note is
+// written, it seems that SourceForge has reset their tracking numbers at least
+// once, so I don't know how to recover those old tickets. The ticket numbers
+// for the following tests are from our SVN/Trac repo.
+
+//"narrowing conversion error with -std=c++0x in operator< with int_type != int"
+BOOST_AUTO_TEST_CASE( ticket_5855_test )
+{
+ // The internals of operator< currently store a structure of two int_type
+ // (where int_type is the component type of a boost::rational template
+ // class) and two computed types. These computed types, results of
+ // arithmetic operations among int_type values, are either int_type
+ // themselves or a larger type that can implicitly convert to int_type.
+ // Those conversions aren't usually a problem. But when an arithmetic
+ // operation involving two values of a built-in scalar type smaller than int
+ // are involved, the result is an int. But the resulting int-to-shorter
+ // conversion is considered narrowing, resulting in a warning or error on
+ // some compilers. Notably, C++11 compilers are supposed to consider it an
+ // error.
+ //
+ // The solution is to force an explicit conversion, although it's otherwise
+ // not needed. (The compiler can rescind the narrowing warning if the
+ // results of the larger type still fit in the smaller one, and that proof
+ // can be generated at constexpr time.)
+ typedef short shorter_than_int_type;
+ typedef boost::rational<shorter_than_int_type> rational_type;
+
+ bool const dummy = rational_type() < rational_type();
+
+ BOOST_REQUIRE( not dummy );
+}
+
 BOOST_AUTO_TEST_SUITE_END()


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk