Boost logo

Boost :

Subject: Re: [boost] [Review] Formal Review of Proposed Boost.Ratio LibraryFINAL DAY
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2010-10-11 13:26:21


Hi,

I have implemented the emulation of ratio arithmetic template aliases using inheritance.

While this cover most of the cases, as e.g. boost::ratio_add< boost::ratio<1,2>, boost::ratio<1,3> > could be seen as a ratio<5,6>, as in

boost::intmax_t func(boost::ratio<5,6> s) {
  return s.num;
}

boost::intmax_t test_conversion() {
  return func(boost::ratio_add<boost::ratio<1,2>, boost::ratio<1,3> >() );
}

there are some cases where the matching doesn't works. Next follows an example

template <typename T, typename R>
struct S {
  T val;
};
boost::intmax_t func(S<int, boost::ratio<5,6> > const& s) {
  return s.val*3;
}

boost::intmax_t test() {
  return func(
    S<int, boost::ratio_add<
      boost::ratio<1,2>,
      boost::ratio<1,3>
>
   // ::type // (1)
>()
  );
}

with the following error:

ratio_arithmetic\ratio_add_3.fail.cpp:27: error: invalid initialization of reference of type 'const S<int, boost::ratio<5ll, 6ll> >&' from expression of type 'S<int, boost::ratio_add<boost::ratio<1ll, 2ll>, boost::ratio<1ll, 3ll> > >'

In order to make this work we need yet to add the ::type in line (1). I hope this will not be critical limitation :)

Respect to the constructors and assignement extensions, here is an example that works with the extension (uncomment the #define) and fails otherwise

//#define BOOST_RATIO_EXTENSIONS
#include <boost/ratio.hpp>

boost::intmax_t func(boost::ratio<5,6> const& s) {
  return s.num;
}
boost::intmax_t test() {
  return func(boost::ratio<10,12>());
}

with the error:
ratio_ratio\ratio4.fail.cpp:14: error: invalid initialization of reference of type 'const boost::ratio<5ll, 6ll>&' from expression of type 'boost::ratio<10ll, 12ll>'

The alternative is to add ::type to force normalized ratios

boost::intmax_t func(boost::ratio<5,6> const& s) {
  return s.num;
}
boost::intmax_t test() {
  return func(boost::ratio<10,12>::type());
}

Best,
_____________________
Vicente Juan Botet Escribá
http://viboes.blogspot.com/


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk