std thread sleep_for不适用于某些chrono :: duration

时间:2015-02-24 19:50:20

标签: c++ c++11 chrono stdthread

我正在使用VS2012,我遇到以下示例的问题:

#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 个答案:

答案 0 :(得分:2)

是VS2012编译器问题。不是100%确定它是this one(诉Praetorian)。但是gcc 4.9.2可以毫无问题地编译。在我问之前,我应该考虑尝试一下。