|
Boost : |
From: Jeff Garland (jeff_at_[hidden])
Date: 2005-04-16 17:10:24
On Sat, 16 Apr 2005 22:11:07 +0100, Andy Little wrote
> "Bob Bell" <belvis_at_[hidden]> wrote
>
> > I'd rather approach it the other way around. Instead of saying,
> > "Let's provide various constructors in case someone wants them",
> > we should say "Let's provide a constructor if we know there is a
> > demonstrated, reasonable need for it." Is there a reasonable need for
> > mutliple constructors which allow a user to specify time values in
> > multiple ways?
> >
> > As for ambiguity, there's ambiguity to the compiler, which can
> > probably be worked around, and then there's ambiguity to a human,
> > which can be much more difficult to deal with. In another post, I
> > wrote as an example:
> >
> > timeout t(100);
> >
> > Is this 100 seconds? 100 milliseconds? 100 microseconds? With a
> > single constructor using a type like double with a very simple
> > meaning (i.e., seconds), there is no reader ambiguity, and the
> > interface becomes very simple. At the same time, implementing it
> > (converting to the underlying platform representation) is trivial.
> > If it were me, I'd think long and hard about whether any other
> > constructors are really necessary.
>
> Sounds like you need a UDT for time.
>
> See http://www.servocomm.freeserve.co.uk/Cpp/physical_quantity/index.html
>
> time::ys to time::yr ( 1 yoctosecond = 1e-24 s)
>
> OTOH I believe the boost date_time lib also has various udts for time
Yep. The generalized type is time_duration, but there a bunch of unit types
so you can say things like:
time_duration td = seconds(10) + milliseconds(20) - nanoseconds(100);
So you can write your signature as:
void f(boost::posix_time::time_duration td);
and your clients can use all the other types to make their code clear.
Jeff
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk