我偶然发现了一个奇怪的错误
C2440:'':无法从'_CR'转换为'std :: chrono :: milliseconds'
基本上以Howard Hinnant's中的another question代码为基础。
这应该在Visual Studio 2012 RC上编译吗?这个问题会是什么原因?修复或解决方法怎么样?我的目的只是创建一个简单的计时器(没有太严重),所以如果存在这种效果的东西,将采取点 - 以及其他实现提示。
有问题的代码如下。 用法:
timers::stopwatch w;
w.start();
std::cout << to_milliseconds(w.elapsed()) << std::endl;
头文件是(为了简洁而省略了实现)
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(); }
}
根据Howard Hinnant的说明,标题,实现文件和调用文件中有#include <chrono>
。该错误指向MS <chrono>
标头,并在此代码中触发错误
// 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))));
}
,特别是<chrono>
的第573行,前面提到的是
[...]
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)));
如果我的代码中的某些内容或VC ++ <chrono>
标头中存在某些内容,那么我无法真正遵循所有代码(至少)的目的。我认识到来自TC1/SC22/WG21 n2661的一些命名。无论如何,我稍后会回到计算机,看看将stopwatch
隔离到自己的项目是否会影响此错误的发生。
我把代码放到一个空项目中,问题仍然存在。作为补充说明,对于三个to_ *秒调用,存在六个编译器错误。如果我将方便方法放在注释中,代码就会编译并运行。
嗯,我想知道什么是好的解决方法(或修复)......?
答案 0 :(得分:7)
我认为编译时错误是由Visual Studio 2012 RC中的这个错误引起的:
要解决此问题,请使用基于整数类型的持续时间而不是浮点数:
#include <cstdint>
//...
typedef std::chrono::duration<std::uint64_t, period> duration;
答案 1 :(得分:0)
如果我添加:
,您的代码将为我编译#include <chrono>
到它。我无权访问Visual Studio 2012 RC,因此我无法调查此错误的原因,甚至无法确定它发生的位置。