2015-02-24 38 views
1

我使用VS2012和我有以下例题:STD线程sleep_for行不通了一些时辰::持续时间

#include <chrono> 
#include <thread> 

int main() 
{ 
    // doesn't compile and I don't understand why: 
    std::this_thread::sleep_for(std::chrono::duration<double>(0.1)); 

    // I can use this but still I would like to know the reason: 
    std::this_thread::sleep_for(std::chrono::duration<long long, std::milli>(100)); 

    return 0; 
} 

两个时间应该是有效的。并且可以在不同的环境中使用它们。

编译错误:

1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\chrono(749): error C2679: binary '+=' : no operator found which takes a right-hand operand of type 'const std::chrono::duration<_Rep>' (or there is no acceptable conversion) 
1>   with 
1>   [ 
1>    _Rep=double 
1>   ] 
1>   c:\program files (x86)\microsoft visual studio 11.0\vc\include\chrono(166): could be 'std::chrono::duration<_Rep,_Period> &std::chrono::duration<_Rep,_Period>::operator +=(const std::chrono::duration<_Rep,_Period> &)' 
1>   with 
1>   [ 
1>    _Rep=__int64, 
1>    _Period=std::nano 
1>   ] 
1>   while trying to match the argument list '(std::chrono::nanoseconds, const std::chrono::duration<_Rep>)' 
1>   with 
1>   [ 
1>    _Rep=double 
1>   ] 
1>   c:\program files (x86)\microsoft visual studio 11.0\vc\include\thread(164) : see reference to function template instantiation 'xtime std::_To_xtime<double,std::ratio<_Nx>>(const std::chrono::duration<_Rep> &)' being compiled 
1>   with 
1>   [ 
1>    _Nx=0x01, 
1>    _Rep=double 
1>   ] 
1>   i:\prog\.c++\test2\test2\source.cpp(13) : see reference to function template instantiation 'void std::this_thread::sleep_for<double,std::ratio<_Nx>>(const std::chrono::duration<_Rep> &)' being compiled 
1>   with 
1>   [ 
1>    _Nx=0x01, 
1>    _Rep=double 
1>   ] 
1> 
1>Build FAILED. 

任何帮助表示赞赏。

+1

'milli'需要'std :: milli',否则你的代码似乎有效。这是[MSVC错误](https://connect.microsoft.com/VisualStudio/feedback/details/752794/std-chrono-duration-cast-lacks-double-support)。奇怪的是,即使这个bug被修复了,我仍然可以在VS2013上重现这个问题。 – Praetorian 2015-02-24 20:21:04

+0

谢谢我编辑过(std :: milli)显然是复制粘贴错误。 – Sekory 2015-02-24 20:33:53

回答

2

这是VS2012编译器问题。不是100%确定它是否是this one(thx Praetorian)。但是可以在没有gcc 4.9.2问题的情况下编译。在我问之前,我应该考虑尝试一下。