Visual Studio uses wrong template function for chrono :: duration division
I converted some code to use the C ++ 11 chrono library instead of using the ctime library, at least in part, to better understand the chrono library. Most of them have done a great job, with the exception of trying to separate chronological :: durations. I reduced the offending code to a simple example and it took me a while to figure out why it was giving me the error.
#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 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 tries to determine a generic type between milliseconds and long. Compiler output:
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? Is this a standard C ++ 11 problem?
source to share
Your example code compiles for me using clang / libc ++. And your description of what should be happening sounds right to me. Also, if I print out numTicks
, I get 12 which equals 200/16 (integer division).
Sounds like a visual stdio error to me. I don't see anything wrong with your code.
source to share