简体   繁体   English

计时代码“C2440:”中的一个奇怪错误 <function-style-cast> &#39;:无法从&#39;_CR&#39;转换为&#39;std :: chrono :: milliseconds&#39;“

[英]A strange error in chrono code “C2440: '<function-style-cast>' : cannot convert from '_CR' to 'std::chrono::milliseconds'”

I stumbled upon a strange error 我偶然发现了一个奇怪的错误

C2440: '' : cannot convert from '_CR' to 'std::chrono::milliseconds' C2440:'':无法从'_CR'转换为'std :: chrono :: milliseconds'

in what amounts basically to Howard Hinnant's code in another question . 另一个问题中基本上是霍华德·亨南特的代码。

Should this compile on Visual Studio 2012 RC? 这应该在Visual Studio 2012 RC上编译吗? What would be the reason for this problem? 这个问题会是什么原因? How about a fix or a workaround? 修复或解决方法怎么样? My aim is just to create a simple timer (nothing too serious), so if there exists something to that effect, point will be taken -- as well as other implementation cues. 我的目标只是创建一个简单的计时器(没有太严重),所以如果存在这种效果的东西,将采取点 - 以及其他实现提示。

The code in question is as follows. 有问题的代码如下。 Usage: 用法:

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

And the header file is (implementation ommitted for breverity) 头文件是(为了简洁而省略了实现)

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 as per Howard Hinnant's note, there's #include <chrono> in the header, the implementation file and the calling file. 按照Howard Hinnant的说明,标题中的#include <chrono> ,实现文件和调用文件。 The error points to MS <chrono> header and the error is triggered in this code 该错误指向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))));
}

and specifically on line 573 of <chrono> , which on the aforementioned is 特别是在<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)));

I can't really follow the purpose of all of that code (yet, at least) to have a definite opinition if it's something in my code somewhere or in the VC++ <chrono> header. 我不能真正遵循所有代码的目的(至少),如果它在我的代码中的某个地方或VC ++ <chrono>标题中有明确的意义。 I recognise some of naming from TC1/SC22/WG21 n2661 . 我认识到TC1 / SC22 / WG21 n2661的一些命名。 In any event, I'll get back to computer later and see if isolating the stopwatch to a project of its own affects the occurence of this error. 无论如何,我稍后会回到计算机,看看将stopwatch与自己的项目隔离是否会影响此错误的发生。

Edit 2 编辑2

I put the code to an empty project and the problem is still there. 我把代码放到一个空项目中,问题仍然存在。 As an added note, for the three to_*seconds calls there are six compiler errors. 作为补充说明,对于三个to_ *秒调用,存在六个编译器错误。 If I put the convenience methods to comments, the code compiles and runs. 如果我将方便方法放在注释中,代码就会编译并运行。

Hmm, I wonder what would be a good work-around (or fix)..? 嗯,我想知道什么是好的解决方法(或修复)..?

I think that the compile-time error is due to this bug in Visual Studio 2012 RC : 我认为编译时错误是由于Visual Studio 2012 RC中的这个错误:

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

To workaround it, use a duration based on integral type instead of floating point : 要解决此问题,请使用基于整数类型的持续时间而不是浮点数:

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

Your code compiles for me if I add: 如果我添加,您的代码将为我编译:

#include <chrono>

to it. 它。 I do not have access to Visual Studio 2012 RC, so I can not investigate the cause of this error, or even be sure of where it is occurring. 我无法访问Visual Studio 2012 RC,因此我无法调查此错误的原因,甚至无法确定它发生的位置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 C2440 '<function-style-cast> ': 无法从 'char' 转换为 'std::string'</function-style-cast> - Error C2440 '<function-style-cast>': cannot convert from 'char' to 'std::string' c ++:错误C2440:“ <function-style-cast> &#39;:无法从&#39;A转换 <TYPE> &#39;至&#39;B <TYPE> &#39; - c++:error C2440: '<function-style-cast>' : cannot convert from 'A<TYPE>' to 'B<TYPE>' 错误C2440:“ <function-style-cast> &#39;:无法从“ LinkedList”转换 <int> :: Node *&#39;到&#39;LinkedListIterator <const T> &#39; - error C2440: '<function-style-cast>' : cannot convert from 'LinkedList<int>::Node *' to 'LinkedListIterator<const T>' 错误 C2664 '<function-style-cast> ': 无法从 'CHAR [260]' 转换为 'std::wstring'</function-style-cast> - Error C2664 '<function-style-cast>': cannot convert from 'CHAR [260]' to 'std::wstring' 错误C2440:“类型转换”:无法从“ std :: _ Vector_iterator &lt;_Ty,_Alloc&gt;”转换为“ DWORD” - error C2440: 'type cast' : cannot convert from 'std::_Vector_iterator<_Ty,_Alloc>' to 'DWORD' 错误C2440:&#39;type cast&#39;:无法从&#39;overloaded-function&#39;转换为&#39;HOOKPROC&#39; - error C2440: 'type cast' : cannot convert from 'overloaded-function' to 'HOOKPROC' <function-style-cast>错误:无法从“初始化列表”转换为“ std :: thread” - <function-style-cast> error: Cannot convert from 'initializer list' to 'std::thread' 错误C2440:“ =”:无法从“ std :: list”转换 <std::string,std::allocator<_Ty> &gt; *&#39;到&#39;std :: string *&#39; - error C2440: '=': cannot convert from 'std::list<std::string,std::allocator<_Ty>> *'to 'std::string*' 错误C2440:“正在初始化”:无法从“ std :: _ A_iterator &lt;_B&gt;”转换为“ std :: _ A_iterator &lt;_B&gt;” - error C2440: 'initializing' : cannot convert from 'std::_A_iterator<_B>' to 'std::_A_iterator<_B>' 错误C2440:'=':无法从'std :: string []'转换为'std :: string []' - error C2440: '=' : cannot convert from 'std::string []' to 'std::string []'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM