如何解决无缝混合搭配 boost::units::quantity<time> 和 std::chrono::duration
std::chrono::duration
和 boost::units::quantity<time>
都代表相同的物理维度。我正在寻找的是一种使用这些可互换类型值的方法,例如在需要 quantity<time>
的地方使用 std::clock::duration
,反之亦然。
请注意,我已经有了转换功能,但是手动转换感觉很笨拙。编译器应该能够自动进行转换。
一个例子是将 chrono_literals 分配给一个数量:
quantity<time> q = 2h;
我的电流转换功能:
/// <summary>
/// Convert a std::chrono::duration to a boost::unit time quantity
/// </summary>
/// <typeparam name="_Period1">a std::ratio representing the tick period (i.e. the number of seconds per tick)</typeparam>
/// <typeparam name="_Type">an arithmetic type representing the number of ticks</typeparam>
/// <param name="in">the duration to be converted</param>
/// <returns>the time quantity representing the duration</returns>
template <class _Period1,class _Type> constexpr boost::units::quantity<boost::units::si::time> toBoostTime(std::chrono::duration<_Type,_Period1> in) {
constexpr auto milli = boost::units::si::milli;
constexpr auto sec = boost::units::si::seconds;
return boost::units::quantity<boost::units::si::time>(std::chrono::duration_cast<std::chrono::milliseconds>(in).count() * milli * sec);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。