Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sadly VS2012's duration_cast is broken, and I actually need the functionality which is broken. So, I wrote my own:

template<typename ToUnit, typename Rep, typename Period>
ToUnit duration_cast_2(const std::chrono::duration<Rep, Period>& right)
{
    typedef std::ratio_divide<Period, typename ToUnit::period>::type ratio;
    typedef std::common_type<std::common_type<typename ToUnit::rep, Rep>::type, intmax_t>::type common_type;
    return ToUnit(static_cast<typename ToUnit::rep>(static_cast<common_type>(right.count()) * static_cast<common_type>(ratio::num) / static_cast<common_type>(ratio::den)));
}

But I'm not entirely confident that it's correct.

share|improve this question
Links don;t work for me. What is a duration_cast<> supposed to do? – Loki Astari Feb 18 at 16:06
@LokiAstari Turn a std::chrono::duration<long long, std::micro> into a std::chrono::duration<double, std::milli>, for example. That conversion cannot be done implicitly, and needs the cast. – Dave Feb 18 at 16:28

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.