// in all copies. This software is provided "as is" without express or
// implied warranty, and with no claim as to its suitability for any purpose.
-// See http://www.boost.org for most recent version including documentation.
+// See http://www.boost.org/libs/rational for documentation.
// Credits:
// Thanks to the boost mailing list in general for useful comments.
// Nickolay Mladenov, for the implementation of operator+=
// Revision History
+// 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)
+// 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)
// 03 Mar 01 Workarounds for Intel C++ 5.0 (David Abrahams)
// 05 Feb 01 Update operator>> to tighten up input syntax
#include <cstdlib> // for std::abs
#include <boost/call_traits.hpp> // for boost::call_traits
#include <boost/config.hpp> // for BOOST_NO_STDC_NAMESPACE, BOOST_MSVC
+#include <boost/detail/workaround.hpp> // for BOOST_WORKAROUND
namespace boost {
dividable < rational<IntType>,
addable2 < rational<IntType>, IntType,
subtractable2 < rational<IntType>, IntType,
+ subtractable2_left < rational<IntType>, IntType,
multipliable2 < rational<IntType>, IntType,
dividable2 < rational<IntType>, IntType,
+ dividable2_left < rational<IntType>, IntType,
incrementable < rational<IntType>,
decrementable < rational<IntType>
- > > > > > > > > > > > > > >
+ > > > > > > > > > > > > > > > >
{
- typedef IntType int_type;
typedef typename boost::call_traits<IntType>::param_type param_type;
+ struct helper { IntType parts[2]; };
+ typedef IntType (helper::* bool_type)[2];
+
public:
+ typedef IntType int_type;
rational() : num(0), den(1) {}
rational(param_type n) : num(n), den(1) {}
rational(param_type n, param_type d) : num(n), den(d) { normalize(); }
// Operator not
bool operator!() const { return !num; }
+ // Boolean conversion
+
+#if BOOST_WORKAROUND(__MWERKS__,<=0x3003)
+ // The "ISO C++ Template Parser" option in CW 8.3 chokes on the
+ // following, hence we selectively disable that option for the
+ // offending memfun.
+#pragma parse_mfunc_templ off
+#endif
+
+ operator bool_type() const { return operator !() ? 0 : &helper::parts; }
+
+#if BOOST_WORKAROUND(__MWERKS__,<=0x3003)
+#pragma parse_mfunc_templ reset
+#endif
+
// Comparison operators
bool operator< (const rational& r) const;
bool operator== (const rational& r) const;
return operator/= (rational<IntType>(i));
}
-// Intel C++ seems to choke on this unless i is a reference parameter, matching
-// the reference parameter in the operator-() generated by subtractable
-template <typename IntType, typename T>
-inline rational<IntType>
-operator- (const T& i, const rational<IntType>& r)
-{
- IntType ii = i; // Must be able to implicitly convert T -> IntType
- return rational<IntType>(ii) -= r;
-}
-
-// Intel C++ seems to choke on this unless i is a reference parameter, matching
-// the reference parameter in the operator-() generated by subtractable
-template <typename IntType, typename T>
-inline rational<IntType>
-operator/ (const T& i, const rational<IntType>& r)
-{
- IntType ii = i; // Must be able to implicitly convert T -> IntType
- return rational<IntType>(ii) /= r;
-}
-
// Increment and decrement
template <typename IntType>
inline const rational<IntType>& rational<IntType>::operator++()
// Type conversion
template <typename T, typename IntType>
-inline T rational_cast(const rational<IntType>& src)
+inline T rational_cast(
+ const rational<IntType>& src BOOST_APPEND_EXPLICIT_TEMPLATE_TYPE(T))
{
return static_cast<T>(src.numerator())/src.denominator();
}