将 chrono::milliseconds 转换为 uint64_t?
Cast chrono::milliseconds to uint64_t?
假设我在变量 x
:
中有毫秒数
chrono::milliseconds x = std::chrono::duration_cast<chrono::milliseconds>(something);
如何将 x
从 chrono::milliseconds
转换为 uint64_t
?
我试过:
uint64_t num = std::chrono::duration_cast<uint64_t>(x);
但上面写着:
no matching function for call to
duration_cast(std::chrono::milliseconds&)
首先,你通常不应该做这种事情。 <chrono>
提供了一个类型安全的通用单位库来处理持续时间,没有什么好的理由可以避免这种安全性和通用性。
类型安全的通用单元库不会发生而类型不安全的整数类型会发生的一些问题示例:
// a type-safe units library prevents these mistakes:
int seconds = ...
int microseconds = seconds * 1000; // whoops
int time = seconds + microseconds; // whoops
void bar(int seconds);
bar(microseconds); // whoops
// a generic duration type prevents the need for:
unsigned sleep(unsigned seconds);
int usleep(useconds_t useconds);
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
int attosleep(long long attoseconds); // ???
// just use:
template<typename Duration>
int sleep_for(Duration t); // users can specify sleep in terms of hours, seconds, microseconds, femetoseconds, whatever. Actual sleep duration depends on QoI, as always.
一个很好的理由的例子是与第三方库的兼容性,第三方库做出了不在其 API 中使用类型安全的通用单元库的不幸决定。在这种情况下,转换应尽可能靠近 API 边界进行,以尽量减少使用不安全类型的程度。
话虽如此,当您确实有充分的理由时,您可以这样做:
std::chrono::milliseconds x = ...
std::uint64_t num = x.count();
请记住,chrono::milliseconds
等预定义计时持续时间使用带符号的表示形式,因此您需要注意确保该值适合转换为 uint64_t
.
std::chrono::duration_cast
的原型是:
template <class ToDuration, class Rep, class Period>
constexpr ToDuration duration_cast(const duration<Rep,Period>& d);
您无法直接获得 uint64_t
,因为它会转换持续时间 (duration_cast)。所以你需要用 std::uint64_t
.
创建一个 std::duration
using cast = std::chrono::duration<std::uint64_t>;
std::uint64_t ticks = std::chrono::duration_cast< cast >(something).count();
假设我在变量 x
:
chrono::milliseconds x = std::chrono::duration_cast<chrono::milliseconds>(something);
如何将 x
从 chrono::milliseconds
转换为 uint64_t
?
我试过:
uint64_t num = std::chrono::duration_cast<uint64_t>(x);
但上面写着:
no matching function for call to duration_cast(std::chrono::milliseconds&)
首先,你通常不应该做这种事情。 <chrono>
提供了一个类型安全的通用单位库来处理持续时间,没有什么好的理由可以避免这种安全性和通用性。
类型安全的通用单元库不会发生而类型不安全的整数类型会发生的一些问题示例:
// a type-safe units library prevents these mistakes:
int seconds = ...
int microseconds = seconds * 1000; // whoops
int time = seconds + microseconds; // whoops
void bar(int seconds);
bar(microseconds); // whoops
// a generic duration type prevents the need for:
unsigned sleep(unsigned seconds);
int usleep(useconds_t useconds);
int nanosleep(const struct timespec *rqtp, struct timespec *rmtp);
int attosleep(long long attoseconds); // ???
// just use:
template<typename Duration>
int sleep_for(Duration t); // users can specify sleep in terms of hours, seconds, microseconds, femetoseconds, whatever. Actual sleep duration depends on QoI, as always.
一个很好的理由的例子是与第三方库的兼容性,第三方库做出了不在其 API 中使用类型安全的通用单元库的不幸决定。在这种情况下,转换应尽可能靠近 API 边界进行,以尽量减少使用不安全类型的程度。
话虽如此,当您确实有充分的理由时,您可以这样做:
std::chrono::milliseconds x = ...
std::uint64_t num = x.count();
请记住,chrono::milliseconds
等预定义计时持续时间使用带符号的表示形式,因此您需要注意确保该值适合转换为 uint64_t
.
std::chrono::duration_cast
的原型是:
template <class ToDuration, class Rep, class Period>
constexpr ToDuration duration_cast(const duration<Rep,Period>& d);
您无法直接获得 uint64_t
,因为它会转换持续时间 (duration_cast)。所以你需要用 std::uint64_t
.
std::duration
using cast = std::chrono::duration<std::uint64_t>;
std::uint64_t ticks = std::chrono::duration_cast< cast >(something).count();