Boost logo

Boost :

From: Darren Garvey (darren.garvey_at_[hidden])
Date: 2008-06-18 23:48:35


2008/6/18 Beman Dawes <bdawes_at_[hidden]>:

> Beman Dawes wrote:
>
>> A slightly updated version of
>> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2615.html was
>> accepted at the C++ committee meeting last week.
>
>
>> <snip>
>
>
>> I've emailed Howard to see if his prototype implementation can be made
> available with the Boost license. The answer was yes, but he points out that
> an independent implementation would be better in terms of flushing out
> issues with the proposed text for the standard.
>
> The proposal has several components:
>
> <snip>

> * ratio: "ratio is a general purpose utility inspired by Walter Brown
> allowing one to easily and safely compute rational values at compile time."
> The proposal puts this in the Utilities chapter of the standard library. It
> has a lot of similarity to type traits.
>

I thought this bit looked simple enough to have a go at, so I took a shot at
it and am attaching the results (tested with gcc 4.2.3 on Ubuntu). My
conclusion is that the (relevant bit of) text is as clear as it needs to be
- since I could follow it - except for one point:

When it says:
"""
num shall have the value sign(N)*sign(D)*abs(N)/gcd.

den shall have the value abs(D)/gcd.

"""

... are sign() and abs() supposed to be runtime functions? I implemented
them as meta-functions - I'm sure these are in boost somewhere, I just don't
know where. Maybe compilers are generally clever enough, or the standard
guarantees those functions will be evaluated at compile time? I'm never sure
where the line is drawn on this one...

The attached code is quite straightforward and basically does what the text
says it should (I hope), but no more. The main limitation is that it uses
static_gcd<> from Boost.Math. That meta-function is parametrised with
`unsigned long` instead of `intmax_t` which limits how big the allowed
numbers can be. I tried without luck to change the relevant header but I
suppose it's not really relevant to this experiment.

One other thing that threw me:

"""

The proposal is entirely an add-on. It will break no existing C++03 code,
subject to the usual caveat that user code giving a using namespace std may
see name clashes. Even that possibility is mitigated because the time
portion of the proposal is in a std sub-namespace.

"""

I had no idea that it was a given that `using namespace std` could cause
name clashes... Oh well, good thing I don't do that. :)

Anyway, I hope Boost gets a high-resolution timer quick-sharp; it's long
overdue imho.

Kind regards,

Darren


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