Darinth
Darinth

Reputation: 510

Visual studio using the wrong template function for chrono::duration division

I was converting over some code to use the c++11 chrono library rather than using the ctime library, at least in part to get a better understanding on the chrono library. Most of it has gone great, except for trying to do division by two chrono::durations. I've reduced the offending code down to a simple example and it took me a while to figure out why it was giving me the error it is.

#include <chrono>
using namespace std;

int main()
{
    chrono::milliseconds tickLength(16);
    chrono::milliseconds futureDuration(200);

    auto numTicks = futureDuration / tickLength;
}

This should access the function

template<class _Rep1,
    class _Period1,
    class _Rep2,
    class _Period2> inline
    typename common_type<_Rep1, _Rep2>::type
        operator/(
            const duration<_Rep1, _Period1>& _Left,
            const duration<_Rep2, _Period2>& _Right)

but instead appears to be trying to use

template<class _Rep1,
    class _Period1,
    class _Rep2> inline
    typename enable_if<is_convertible<_Rep2,
        typename common_type<_Rep1, _Rep2>::type>::value
        && !_Is_duration<_Rep2>::value,
        duration<typename common_type<_Rep1, _Rep2>::type, _Period1> >::type
        operator/(
            const duration<_Rep1, _Period1>& _Left,
            const _Rep2& _Right)

and thus is trying to determine a common type between milliseconds and long long. The compiler output is:

1>------ Build started: Project: Playground, Configuration: Debug Win32 ------
1>  playground.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\type_traits(1446): error C2446: ':' : no conversion from 'std::chrono::milliseconds' to '__int64'
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>          c:\users\XXX\documents\visual studio 2013\projects\playground\playground\playground.cpp(9) : see reference to class template instantiation 'std::common_type<__int64,std::chrono::milliseconds>' being compiled
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Am I doing something wrong in my code? Is this a visual studio issue? Is this a c++11 standard issue?

Upvotes: 3

Views: 625

Answers (1)

Howard Hinnant
Howard Hinnant

Reputation: 219345

Your example code compiles for me using clang/libc++. And your description of what should be happening sounds right to me. Further, if I print out numTicks, I get 12, which is 200/16 (integer division).

Sounds like a visual stdio bug to me. I see nothing wrong with your code.

Upvotes: 2

Related Questions