是否有任何优雅的方法将升压chrono time_point转换为等效的标准库?
答案 0 :(得分:1)
恐怕除非您接受boost::chrono::steady_clock
和std::chrono::steady_clock
具有相同纪元的先验条件,否则您不能保证会进行转换。如果您可以接受,则可以执行以下操作:
#include <boost/chrono.hpp>
#include <chrono>
/* Convert from boost::ratio to std::ratio */
template <typename T> struct conv_ratio;
template <std::intmax_t N, std::intmax_t D>
struct conv_ratio<boost::ratio<N, D>> {
using type = std::ratio<N, D>;
};
template <typename T>
using conv_ratio_t = typename conv_ratio<T>::type;
/* Convert from boost::duration to std::duration */
template <typename T> struct conv_duration;
template <typename Rep, typename Period>
struct conv_duration<boost::chrono::duration<Rep, Period>> {
using type = std::chrono::duration<Rep, conv_ratio_t<Period>>;
};
template <typename T>
using conv_duration_t = typename conv_duration<T>::type;
/* Convert from A::time_point to B::time_point. This assumes that A
* and B are clocks with the same epoch. */
template <typename A, typename B>
typename B::time_point convert_timepoint_same_clock(
typename A::time_point const & tp)
{
return typename B::time_point(
conv_duration_t<typename A::time_point::duration>(
tp.time_since_epoch().count()));
}
int main()
{
auto now_boost = boost::chrono::steady_clock::now();
auto now_std = convert_timepoint_same_clock<
boost::chrono::steady_clock, std::chrono::steady_clock>(now_boost);
}