Re: [datetime] conversion operators are all wrong
Alberto Ganesh Barbati wrote:
Hi,
(this post should be directed to comp.std.c++, but since it's still
down...)
referring to the latest public draft, paper N2521, the new section
[datetime] contains specifications for types nanoseconds, microseconds,
etc. All those types (except nanoseconds) have conversion operators to
types with greater resolution (i.e.: hours to minutes, minutes to
microseconds, etc.) Unfortunately all those methods are specified
incorrectly. For example method microseconds::operator nanoseconds() is
specified as:
Returns: count() * (nanoseconds::ticks_per_second /
microseconds::ticks_per_second)
while it should be
Returns: nanoseconds(count() * (nanoseconds::ticks_per_second /
microseconds::ticks_per_second))
The latter should have the same semantics as the former, assuming
tick_type (which is implementation-defined) is implicitly convertible to
long long. Adding the explicit type conversion implies that tick_type
need not be implicitly convertible to long long. As a simplified
example, given the following approximation to the draft:
typedef struct tick {
tick(long) { }
operator long() { return 1L; }
} tick_type;
struct nanoseconds {
nanoseconds(long) { }
};
struct microseconds {
typedef tick tick_type;
operator nanoseconds() { return tick_type(1000L); }
};
The following does not compile:
int main() {
microseconds m;
nanoseconds n = m;
}
Adding the explicit type conversion would make the code compile. Is the
implementation-defined tick_type required to be implicitly convertible
to long long?
Similarly, for all other conversion operators.
Moreover, method hours::operator minutes() is specified as:
Returns: count() * (hours::seconds_per_tick * minutes::seconds_per_tick)
given that hours::seconds_per_tick is 3600 and minutes::seconds_per_tick
is 60, even the computation is wrong!
Nice catch.
The correct specification should be:
Returns: minutes(count() * (hours::seconds_per_tick /
minutes::seconds_per_tick))
Same as above re. the explicit type conversion.
Similarly, the specifications of hours::operator seconds() and
minutes::operator seconds() are also wrong, although in those cases the
computation is accidentally correct because seconds::seconds_per_tick == 1.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]