
Boost : 
Subject: Re: [boost] [MPL.Math] ratio
From: Cromwell Enage (sponage_at_[hidden])
Date: 20081114 04:06:28
 On Thu, 11/13/08, Howard Hinnant wrote:
> On Nov 13, 2008, at 3:55 PM, Beman Dawes wrote:
> > Howard would have to comment on that; I've pinged
> > him to make him aware of this thread.
> >
> > The Boost implementation can add extensions, and we
> > can submit a comment on the CD. If it is as simple
> > as a typedef, it stands a good chance of being
> > accepted. If you write it up, with a proposed
> > resolution, I'll submit it.
>
> My only comment so far is that these look like good
> suggestions. A defect report should show a motivating code
> sample for best chances.
>
> Howard
Sections: 20.4.1 [Class template ratio], 20.4.2 [Arithmetic on ratio types]
Discussion:
Let's say that client code needs to use a unary metafunction named absolute_ratio in the following type definitions, to be evaluated lazily.
// Usage.
typedef std::ratio<1,2>
negative_one_half;
typedef absolute_ratio<negative_one_half>
one_half;
typedef absolute_ratio<
std::ratio_multiplies<
one_half
, negative_one_half
>
>
one_fourth;
// End usage.
Assuming that a (partial) specialization of boost::mpl::negate exists for std::ratio, a programmer should be able to define absolute_ratio this way.
template <typename R>
struct absolute_ratio
: boost::mpl::eval_if<
R::type::num < 0
, boost::mpl::negate<R>
, R
>
{
};
The expression R::type::num is necessary because the std::ratio_multiplies metafunction is not required to inherit from std::ratio. However, the expression will not compile when it sees that std::ratio is not a metafunction.
Proposed resolution:
Add the nullary metafunction type definition to the ratio class template.
namespace std {
template <intmax_t N, intmax_t D = 1>
class ratio
{
public:
typedef ratio<N,D> type;
static const intmax_t num;
static const intmax_t den;
};
}
We should also require that each of the arithmetic metafunctions inherit from std::ratio if we want to allow the following usage (note the change from R::type::num to R::num).
template <typename R>
struct absolute_ratio
: boost::mpl::eval_if<
R::num < 0
, boost::mpl::negate<R>
, R
>
{
};
Cheers,
Cromwell D. Enage
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk