": cannot be converted from" _CR "to" std :: chrono :: milliseconds " I stumbled upon a...">

Strange error in chrono code "C2440:" <cast-function-style> ": cannot be converted from" _CR "to" std :: chrono :: milliseconds "

I stumbled upon a strange mistake

C2440: '': cannot convert from '_CR' to 'std :: chrono :: milliseconds'

in what basically means the Howard Hinnant code in another question .

Should this compilation on Visual Studio 2012 RC? What is the cause of this problem? How about a fix or workaround? My goal is simply to create a simple timer (nothing too serious), so if there is anything like that, a point will be taken, as well as other implementation hints.

This code is as follows. Using:

  timers::stopwatch w; w.start(); std::cout << to_milliseconds(w.elapsed()) << std::endl; 

And the header file (implementation excluded for brevity)

 namespace timers { class stopwatch { public: typedef std::chrono::high_resolution_clock clock; typedef clock::time_point time_point; typedef clock::period period; typedef std::chrono::duration<float, period> duration; stopwatch(); ~stopwatch(); void start(); void stop(); void restart(); void reset(); duration elapsed() const; private: clock timer_; time_point start_point_; time_point end_point_; bool is_running_; void start_measuring(); void stop_measuring(); }; } //Some convenience stuff... namespace { long long to_milliseconds(timers::stopwatch::duration const& duration) { return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(); } long long to_nanoseconds(timers::stopwatch::duration const& duration) { return std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count(); } long long to_seconds(timers::stopwatch::duration const& duration) { return std::chrono::duration_cast<std::chrono::seconds>(duration).count(); } 

}

Edit

As with Howard Hinnant, there is #include <chrono> in the header, implementation file, and calling file. The error points to the MS <chrono> header, and the error runs in this code

 // duration_cast template<class _To, class _Rep, class _Period> inline typename enable_if<_Is_duration<_To>::value, _To>::type duration_cast(const duration<_Rep, _Period>& _Dur) { // convert duration to another duration typedef typename ratio_divide<_Period, typename _To::period>::type _CF; typedef typename common_type< typename common_type<typename _To::rep, _Rep>::type, intmax_t>::type _CR; if (_CF::num == 1 && _CF::den == 1) return (_To(static_cast<typename _To::rep>(_Dur.count()))); else if (_CF::num != 1 && _CF::den == 1) return (_To(static_cast<typename _To::rep>(static_cast<_CR>(_Dur.count())) * static_cast<_CR>(_CF::num))); else if (_CF::num == 1 && _CF::den != 1) return (_To(static_cast<typename _To::rep>(static_cast<_CR>(_Dur.count()) / static_cast<_CR>(_CF::den)))); else return (_To(static_cast<typename _To::rep>(xtatic_cast<_CR> _Dur.count()) * static_cast<_CR>(_CF::num) / static_cast<_CR>(_CF::den)))); } 

and, in particular, on line 573 of <chrono> , which on the above is

  [...] else if (_CF::num != 1 && _CF::den == 1) return (_To(static_cast<typename _To::rep>(static_cast<_CR>(_Dur.count())) * static_cast<_CR>(_CF::num))); 

I cannot completely follow the purpose of all this code (at least at least) to have a definite opinion if it is something in my code somewhere or in the VC ++ <chrono> . I recognize some names from TC1 / SC22 / WG21 n2661 . In any case, I will return to the computer later and see if the allocation of stopwatch affects stopwatch own project on the occurrence of this error.

Edit 2

I put the code in an empty project and the problem is still there. As an additional note, for three calls up to * * seconds, there are six compiler errors. If I put convenience methods in comments, the code compiles and runs.

Hmm, I wonder what would be good work (or fix) ..?

+4
source share
2 answers

I think the compile-time error is related to this error in Visual Studio 2012 RC:

https://connect.microsoft.com/VisualStudio/feedback/details/752794/std-chrono-duration-cast-lacks-double-support

To get around this, use duration based on integral type instead of floating point:

 #include <cstdint> //... typedef std::chrono::duration<std::uint64_t, period> duration; 
+6
source

Your code compiles for me if I add:

 #include <chrono> 

to him. I do not have access to Visual Studio 2012 RC, so I can not investigate the cause of this error or even be sure where it occurs.

0
source

All Articles